Flip Game(枚举)
~题目链接~
http://poj.org/problem?id=1753
可参考大神题解(位运算):http://www.cnblogs.com/tanhehe/archive/2013/06/11/3131615.html
http://www.cnblogs.com/kuangbin/archive/2011/07/30/2121677.html
http://poj.org/showmessage?message_id=340320
输入
bwwb bbwb bwwb bwww
结果
4
位运算+队列
知识点:
^ 异或,1^1=0,1^0=1
#include<stdio.h> #include<string.h> #include<stdlib.h> #define maxn 65535+10 #include<queue> using namespace std; int data,vis[maxn]; struct node { int rond,step; } N; int sreach() { queue<node>Q; N.rond=data; N.step=0; Q.push(N); vis[N.rond]=1;//标记这种形式的已访问 if(data==0 || data==65535) return N.step; while(!Q.empty()) { for(int i=0; i<16; i++)//从最后一位开始变换,并记录 { N.rond=Q.front().rond^(1<<i);//此位点翻转 if(i<12) N.rond^=1<<(i+4);//此位点上侧点翻转 if(i>=4) N.rond^=1<<(i-4);//此位点下侧点翻转 if(i%4!=0) N.rond^=1<<(i-1);//此位点左侧点翻转 if(i%4!=3) N.rond^=1<<(i+1);//此位点右侧点翻转 if(!vis[N.rond])//判断是否被访问过 { N.step=Q.front().step+1; Q.push(N); vis[N.rond]=1; } if(N.rond==0 || N.rond==65535) return N.step; } Q.pop(); } return -1;//不能转换 } int main() { char s; for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { data*=2; scanf("%c",&s); if(s=='b') data+=1;//转换成数据 } getchar(); } int x=sreach(); if(x==-1) printf("Impossible\n"); else printf("%d\n",x); return 0; }
附带~测试数据(测试数据过,稳过)
bwbw wwww bbwb bwwb Impossible bwwb bbwb bwwb bwww 4 wwww wwww wwww wwww 0 bbbb bbbb bbbb bbbb 0 bbbb bwbb bbbb bbbb Impossible bwbb bwbb bwbb bbbb Impossible bwbb wwwb bwbb bbbb 1 wwww wwwb wwbb wwwb 1 wwww wwww wwwb wwbb 1 wbwb bwbw wbwb bwbw Impossible bbbb bwwb bwwb bbbb 4 bwwb wbbw wbbw bwwb 4 bbww bbww wwbb wwbb Impossible bbwb bbbw wwbb wwwb Impossible wwwb wwbw wbww wwbw Impossible bbbb wwww wwbb wbbb Impossible bwwb wbwb wbbb wbbb 4 bwbb bwbb bwbw bbbw 5 wbwb bbbb bbww wbbb 6 bbwb bbbb wbwb bbbb 5