POJ - 1753 Flip Game(状压枚举)
https://vjudge.net/problem/POJ-1753
题意
4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动。现要求把所有棋子都翻成同一种颜色,问最少需要几步。
分析
同一个棋子翻偶数次等于没有翻,翻奇数次就浪费步数,因此每个棋子最多翻一次,也就是说,答案最大就是16。故总状态数就是2^16,可以直接dfs暴力。还有另一种思路就是状态压缩,把棋盘压成16位的数字,翻转时采用异或操作,我们暴力枚举每个状态,即所有选择棋子的可能情况跑一遍,对于每一个棋子,对其能影响的位置可以预处理出来,这样就通过位运算来模拟翻转过程了,更具体的看代码。
#include<iostream> #include<cmath> #include<cstring> #include<queue> #include<vector> #include<cstdio> #include<algorithm> #include<map> #include<set> #define rep(i,e) for(int i=0;i<(e);i++) #define rep1(i,e) for(int i=1;i<=(e);i++) #define repx(i,x,e) for(int i=(x);i<=(e);i++) #define X first #define Y second #define PB push_back #define MP make_pair #define mset(var,val) memset(var,val,sizeof(var)) #define scd(a) scanf("%d",&a) #define scdd(a,b) scanf("%d%d",&a,&b) #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c) #define pd(a) printf("%d\n",a) #define scl(a) scanf("%lld",&a) #define scll(a,b) scanf("%lld%lld",&a,&b) #define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c) #define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std; typedef long long ll; template <class T> void test(T a){cout<<a<<endl;} template <class T,class T2> void test(T a,T2 b){cout<<a<<" "<<b<<endl;} template <class T,class T2,class T3> void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;} template <class T> inline bool scan_d(T &ret){ char c;int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret = ret*10+(c-'0'); ret*=sgn; return 1; } const int N = 1e6+10; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3fll; const ll mod = 1000000000; int T; void testcase(){ printf("Case %d:",++T); } const int MAXN = 5e5+10 ; const int MAXM = 150; const double eps = 1e-8; const double PI = acos(-1.0); int n; int st[16] = {0x13,0x27,0x4e,0x8c,0x131,0x272,0x4e4,0x8c8,0x1310,0x2720,0x4e40,0x8c80,0x3100,0x7200,0xe400,0xc800}; int po[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}; int g[5][5]; void work(){ char s[6]; int state=0; for(int i=0;i<4;i++){ scanf("%s",s); for(int j=0;j<4;j++){ if(s[j]=='b') state += po[4*i+j]; } } int ans=inf; for(int i=0;i<(1<<16);i++){ int cnt = 0; int temp = state; for(int j=0;j<16;j++){ if(i & po[j]){ temp ^= st[j]; cnt++; } } if(temp==0 || temp == 65535){ if(ans>cnt){ ans=cnt; } } } if(ans==inf) puts("Impossible"); else cout<<ans<<endl; return; } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL // init(); work(); return 0; }
深搜的做法,规定一定的搜索顺序,递归回溯。
#include<iostream> #include<cmath> #include<cstring> #include<queue> #include<vector> #include<cstdio> #include<algorithm> #include<map> #include<set> #define rep(i,e) for(int i=0;i<(e);i++) #define rep1(i,e) for(int i=1;i<=(e);i++) #define repx(i,x,e) for(int i=(x);i<=(e);i++) #define X first #define Y second #define PB push_back #define MP make_pair #define mset(var,val) memset(var,val,sizeof(var)) #define scd(a) scanf("%d",&a) #define scdd(a,b) scanf("%d%d",&a,&b) #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c) #define pd(a) printf("%d\n",a) #define scl(a) scanf("%lld",&a) #define scll(a,b) scanf("%lld%lld",&a,&b) #define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c) #define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std; typedef long long ll; template <class T> void test(T a){cout<<a<<endl;} template <class T,class T2> void test(T a,T2 b){cout<<a<<" "<<b<<endl;} template <class T,class T2,class T3> void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;} template <class T> inline bool scan_d(T &ret){ char c;int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret = ret*10+(c-'0'); ret*=sgn; return 1; } const int N = 1e6+10; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3fll; const ll mod = 1000000000; int T; void testcase(){ printf("Case %d:",++T); } const int MAXN = 5e5+10 ; const int MAXM = 150; const double eps = 1e-8; const double PI = acos(-1.0); int n; int g[5][5]; bool f; bool check(){ int t = g[0][0]; for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(t!=g[i][j]) return false; return true; } void flip(int x,int y){ g[x][y] = 1-g[x][y]; if(x-1>=0) g[x-1][y] = 1-g[x-1][y]; if(y-1>=0) g[x][y-1] = 1-g[x][y-1]; if(x+1<4) g[x+1][y] = 1-g[x+1][y]; if(y+1<4) g[x][y+1] = 1-g[x][y+1]; } void dfs(int x,int y,int state){ if(state==0){ f = check(); return; } if(f||y>3) return; flip(x,y); if(x<3) dfs(x+1,y,state-1); else dfs(0,y+1,state-1); flip(x,y); if(x<3) dfs(x+1,y,state); else dfs(0,y+1,state); return; } void work(){ char s[6]; f=false; for(int i=0;i<4;i++){ scanf("%s",s); for(int j=0;j<4;j++){ if(s[j]=='b') g[i][j]=0; else g[i][j]=1; } } for(n=0;n<=16;n++){ dfs(0,0,n); if(f) break; } if(f) cout<<n<<endl; else puts("Impossible"); return; } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL // init(); work(); return 0; }