Test instructions: Turn the string of topics into 01 sequences, and then, you can do the following conversion 011-->100, 110-->001
Make the sequence 1 minimum
A binary collection represents:
Empty set: 0
Complete Works: (1<<n)-1
Add elements in the collection: s| (1<<i)
Set of deleted elements: s&~ (1<<i)
#include <iostream> #include <algorithm> #include <map> #include <cstdio> #include <cstdlib > #include <vector> #include <cmath> #include <cstring> #include <stack> #include <string > #include <set> #include <fstream>using namespace std, #define PB Push_back#define cl (b) memset (A, B, sizeof (a)) #define Bug printf ("===\n"), #define REP (A, b) for (int i=a;i<b;i++) #define REP_ (A, b) for (int i=a;i<=b;i+ +) #define P pair<int,int> #define X first#define Y second#define vi vector<int>const int MAXN=12;CONST int inf= 999999999;typedef Long Long ll;void Max (Int&a,int b) {if (a<b) a=b;} void Min (Int&a,int b) {if (a>b) a=b;} Char a[maxn];int dp[(1<<MAXN) +1000];int dfs (int n) {if (dp[n]!=-1) return dp[n]; dp[n]=0; for (int i=0;i<12;i++) if (n& (1<<i)) dp[n]++; for (int i=0;i<10;i++) {int t=n; if ((t& (1<<i)) && (t& (1<< (i+1))) &&! ( t& (1<< (i+2)))) {//011 t&=~ (1<<i); T&=~ (1<< (i+1)); T|= (1<< (i+2));//change the sequence of 011 to the Min (Dp[n],dfs (t)); } t=n; if ((t& (1<< (i+2)) && (t& (1<< (i+1))) &&! ( t& (1<<i)) {//110 t|= (1<<i); T&=~ (1<< (1+i)); T&=~ (1<< (i+2));//change the sequence of 110 to 001 Min (Dp[n],dfs (t)); }} return dp[n];} int main () {int n; CL (dp,-1); cin>>n; while (n--) {int x=0; scanf ("%s", a); for (int j=0;j<12;j++) if (a[j]== ' O ') {x|= (1<<j); } printf ("%d\n", DFS (x)); } return 0;} /*5---oo--------o--o-oo-----o----ooo---oooooooooooooooooooooo-o*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVa 10651 Pebble Solitaire (state compression DP)