I don't know what the data for this question is. I handed it in two methods. The first direct recursion, the second memory pruning, and the time turned out to be the same !?
Directly depressed, simple binary represents the State and binary operations.
| 14145176 |
10651 |
Pebble Solitaire |
Accepted |
C ++ |
0.009 |
2014-09-04 09:18:21 |
# Include <cstdio> # include <algorithm> # include <string> # include <cstring> # include <map> # include <iostream> using namespace STD; # define maxd 20 const int max_size = 1 <13; const int INF = 20; int DP [max_size]; int dp (INT state) {If (DP [State]! =-1) return DP [State]; DP [State] = 0; For (INT I = 0; I <12; I ++) if (State & (1 <I) DP [State] ++; For (INT I = 0; I <12; I ++) {if (I> 1) {If (State & (1 <I) & (State & (1 <(I-1 )))&&! (State & (1 <(I-2) {int u = State; // Changes the I-2 digit from 0 to 1, change the I-1 and I bit from 1 to 0 U = u &(~ (1 <I); U = u &(~ (1 <(I-1); U = u | (1 <(I-2); DP [State] = min (DP [State], DP (u) ;}}if (I <10) {If (State & (1 <I )) & (State & (1 <(I + 1 )))&&! (State & (1 <(I + 2) {int u = State; // Changes the I-2 digit from 0 to 1, change the I-1 and I bit from 1 to 0 U = u &(~ (1 <I); U = u &(~ (1 <(I + 1); U = u | (1 <(I + 2); DP [State] = min (DP [State], DP (u) ;}}return DP [State];} int main () {int t; int N; memset (DP,-1, sizeof (DP )); scanf ("% d", & T); While (t --) {char STR [maxd]; scanf ("% s", STR); n = 0; for (INT I = 0; I <strlen (STR); I ++) {If (STR [I] = 'O ') N = n | (1 <I);} int ans = dp (n); printf ("% d \ n", ANS);} return 0 ;}
[Ultraviolet A] 10651-Pebble solitaire (direct recursion or memory)