Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32104 | Accepted: 13988 |
Description
Input
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stdlib.h> #include<queue> #include<stack> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=5+5; const int INF=0x3f3f3f3f; const double EPS=1e-9; int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}}; int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; int map[6][6],step; bool flag; char temp; bool judge() { for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(map[i][j]!=map[0][0]) return false; return true; } void turn(int x,int y) { map[x][y]=!map[x][y]; for(int i=0;i<4;i++) { int xx=x+dir4[i][0]; int yy=y+dir4[i][1]; map[xx][yy]=!map[xx][yy]; } } void DFS(int x,int y,int cur) { if(cur==step) { flag=judge(); return ; } if(x==4||flag) return ; turn(x,y); if(y==3) DFS(x+1,0,cur+1); else DFS(x,y+1,cur+1); turn(x,y); if(y==3) DFS(x+1,0,cur); else DFS(x,y+1,cur); return ; } int main() { //freopen("in.txt","r",stdin); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { scanf("%c",&temp); if(temp=='b') map[i][j]=1; } getchar(); } flag=false; for(step=0;step<=16;step++) { DFS(0,0,0); if(flag) break; } if(flag) cout<<step<<endl; else cout<<"Impossible"<<endl; }