1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namespace std;10 char a[6][6];11 int flag,mi;12 int dir[6][2]={ 0,0,-1,0,1,0,0,-1,0,1};13 int judge()14 {15 for(int i=0;i<4;i++)16 {17 for(int j=0;j<4;j++)18 {19 if(a[i][j]!=a[0][0])20 return 0;21 }22 }23 return 1;24 }25 void fanpai(int x,int y)26 {27 int p,q;28 if(a[x][y]=='b')29 a[x][y]='w';30 else31 a[x][y]='b';32 if (x-1>=0)33 if (a[x-1][y]=='b')34 a[x-1][y]='w';35 else a[x-1][y]='b';36 if (x+1<4)37 if (a[x+1][y]=='b')38 a[x+1][y]='w';39 else a[x+1][y]='b';40 if (y-1>=0)41 if (a[x][y-1]=='b')42 a[x][y-1]='w';43 else a[x][y-1]='b';44 if (y+1<4)45 if (a[x][y+1]=='b')46 a[x][y+1]='w';47 else a[x][y+1]='b';48 /* 49 for(int i=0;i<5;i++)50 {51 p=x+dir[i][0];52 q=y+dir[i][1];53 if(p>=0&&p<4&&q>=0&&q<4)54 {55 if(a[p][q]=='b')56 a[p][q]=='w';57 else58 a[p][q]=='b';59 }60 }*/61 }62 void dfs(int x,int y,int coun)63 {64 if(judge())65 {66 flag=1;67 mi=min(mi,coun);68 return;69 }70 if(x>=4||y>=4) return;71 int nx,ny;72 nx=(x+1)%4;73 ny=y+(x+1)/4;74 dfs(nx,ny,coun);75 fanpai(x,y);76 dfs(nx,ny,coun+1);77 fanpai(x,y);78 return;79 80 }81 int main(int argc, char *argv[])82 {83 84 for(int i=0;i<4;i++)85 scanf("%s",a[i]);86 flag=0;mi=9999999;87 dfs(0,0,0);88 if(flag)89 printf("%d\n",mi);90 else91 printf("Impossible\n");92 return 0;93 }