#include<iostream>
#include<queue>
using namespace std;
int dx[5]={-1,0,0,1,0};
int dy[5]={0,-1,1,0,0};
char chess[4][4];
int lujing=10000000;
bool judge(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(chess[i][j]!=chess[0][0])
return false;
}
}
return true;
}
void fan(int x,int y){
for(int i=0;i<5;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=0&&nx<4&&ny>=0&&ny<4){
if(chess[nx][ny]=='b')
chess[nx][ny]='w';
else
chess[nx][ny]='b';
}
}
}
int jux(int x,int y){
if(y==3)
return x+1;
else
return x;
}
int juy(int x,int y){
if(y==3)
return 0;
else
return y+1;
}
void dfs(int x,int y,bool a,int changdu){
if(a)
fan(x,y);
if(judge()&&changdu<lujing){
lujing=changdu;
}
if(x==3&&y==3){
return;
}
int nx=jux(x,y);
int ny=juy(x,y);
dfs(nx,ny,true,changdu+1);
fan(nx,ny);
dfs(nx,ny,false,changdu);
}
int main()
{
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
cin>>chess[i][j];
}
}
dfs(0,0,true,1);
fan(0,0);
dfs(0,0,false,0);
if(lujing<1000000)
cout<<lujing<<endl;
else
cout<<"Impossible"<<endl;
return 0;
}
Flip Game 棋盘翻转(深搜,暴力)POJ1753
最新推荐文章于 2024-01-31 16:31:39 发布