poj 1753//dfs 有趣的题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
#define inf 0x100000


char aa;
int map[10][10];
int ans;


bool ok(){
    int t = map[0][0];
    for(int i = 0;i<4;++i){
    for(int j = 0;j<4;++j){
    if(t != map[i][j])
    return false;
}
}
return true;
}


void fan(int x,int y){
map[x][y] = !map[x][y];
if(x - 1>= 0)
map[x-1][y] = !map[x-1][y];
if(x+1 < 4)
map[x+1][y] = !map[x+1][y];
if(y - 1>=0)
map[x][y-1] = !map[x][y-1];
if(y + 1 < 4)
map[x][y+1] = !map[x][y+1]; 
}


void dfs(int x,int y,int t){
if(ok()){
if(ans > t)
ans = t;
return;
}
if(x >= 4||y >= 4)
return;
int nx = (x+1)%4;
int ny = y+(x+1)/4;
dfs(nx,ny,t);
fan(x,y);
dfs(nx,ny,t+1);
fan(x,y);
return;
}


int main(){
ans = inf;
memset(map,0,sizeof(map));
for(int i = 0;i<4;++i){
for(int j = 0;j<4;++j){
cin>>aa;
if(aa == 'b')
map[i][j] = 0;
else
map[i][j] = 1;
}
}
dfs(0,0,0);
if(ans == inf)
cout<<"Impossible\n";
else
cout<<ans<<endl;
return 0;
}
 

猜你喜欢

转载自blog.csdn.net/dukig/article/details/79761926