提交时间:2024-01-23 10:01:48
运行 ID: 123863
#include <bits/stdc++.h> using namespace std; bool flag,Map[6][6]; int dir[5][2] = {{-1,0},{1,0},{0,-1},{0,1},{0,0}}; int step; void Flip(int x,int y){ for(int i = 0;i<4;i++){ Map[x+dir[i][0]][y+dir[i][1]] = !Map[x+dir[i][0]][y+dir[i][1]]; } } bool OK(){ for(int i = 1;i<=4;i++){ for(int j = 1;j<=4;j++){ if(Map[i][j]^Map[1][1]) return false; } } return true; } void DFS(int x,int y,int dep){ if(dep==step){ flag = OK(); } if(flag||x==5){ return ; } Flip(x,y); if(y<4) DFS(x,y+1,dep+1); else DFS(x+1,1,dep+1); Flip(x,y); if(y<4) DFS(x,y+1,dep); else DFS(x+1,1,dep); } int main(){ char c; for(int i = 1;i<=4;i++){ for(int j = 1;j<=4;j++){ Map[i][j] = (cin>>c,c=='w'); } } for(step = 0;!flag&&step<=16;++step) DFS(1,1,0); flag?cout<<step-1<<endl:cout<<"Impossible"<<endl; return 0; }