标签:
传送门:http://poj.org/problem?id=1753
思路:16格用16位的int表示,然后用bfs的层次关系枚举;
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map> #define INF 0x3f3f3f3f #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long LL; const int N=100007; int vis[65536],ok; int step[N]; int flip(int state,int i,int j) { state=state^(1<<(4*i+j)); if(i-1>=0) state=state^(1<<((i-1)*4+j)); if(i+1<4) state=state^(1<<((i+1)*4+j)); if(j-1>=0) state=state^(1<<(i*4+j-1)); if(j+1<4) state=state^(1<<(i*4+j+1)); return state; } void bfs(int cs) { queue<int> que; que.push(cs); while(!que.empty()) { int cur=que.front(); que.pop(); if(cur==0||cur==65535) { ok=1; printf("%d\n",step[cur]); break; } for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ int tmp=flip(cur,i,j); if(!vis[tmp]) { vis[tmp]=1; que.push(tmp); step[tmp]=step[cur]+1; } } } } } int main() { mem(vis,0); int state=0; for(int i=0;i<4;i++) { char str[5]; scanf("%s",str); for(int j=0;j<4;j++) { if(str[j]=='b') { state=state|(1<<((4*i)+j)); } } } vis[state]=1; ok=0; bfs(state); if(!ok) { printf("Impossible\n"); } return 0; }
标签:
原文地址:http://blog.csdn.net/code_or_code/article/details/43670807