Title: There is a game like checkers, a total of 12 positions ' o ' represent the pieces '-' stands for the vacancy,
' oo-' can be transformed into '--o ', '-oo ' can be transformed into ' o--', giving you the initial state, and finally, at least a few pieces left.
Analysis: DP, memory search, bit operation. Use the search to be in the neighboring state between the DP.
The optimal solution for each state is that he can transform the optimal solution in all States.
Because, altogether has 2^12 = 4,096 states, each finds the solution namely the storage, does not repeat the computation, therefore the time aspect is not a problem.
A 12-bit integer is used to denote a state, ' o ' is denoted by 1, '-' is represented by 0, then '---oo-------' is 24 (2 binary 000000011000)
(This is from left to right for low to high, upside down)
Through observation can be found that ' oo-' (011) into '--o ' (), '-oo ' (110) converted to ' o--' (001) are corresponding to take the reverse;
The direct use of 7 (2 binary 111) for the XOR, and the corresponding amount can jump the status of 3 (2 011) and 6 (2 binary 110);
Description: Detailed reference code.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <cstdio> #include <cmath>using namespace Std;char buf[13];int f[4100];int dfs (int s {if (F[s] <) return F[s];int Min = 12;for (int i = 0; I < + + i) if (((s>>i) &7) = = 3 | | ((s>>i) &7) = = 6) min = min (min, Dfs (s^ (7<<i))), if (min = =) {for (int i = 0; i <; + + i) min-=! ( s& (1<<i));} return f[s] = Min;} int main () {for (int i = 0; i < 4100; + + i) f[i] = 13;int n,v;while (~scanf ("%d", &n)) for (int i = 1; I <= N; + + i) {scanf ("%s", buf), V = 0;for (int j = 0; J <; + + j) {v <<= 1;if (buf[j] = = ' O ') v + = 1;} printf ("%d\n", DF S (v));} return 0;}
UVa 10651-pebble Solitaire