标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 38749 | Accepted: 16834 |
Description
Input
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 int r[]={-1,1,0,0,0}; 7 int c[]={0,0,-1,1,0}; 8 int chess[6][6]={0}; 9 int step,flag; 10 int ac() 11 { 12 for(int i=1;i<5;i++) 13 { 14 for(int j=1;j<5;j++) 15 { 16 if(chess[i][j]!=chess[1][1]) 17 return 0; 18 } 19 } 20 return 1; 21 } 22 void q(int a,int b) 23 { 24 for(int i=0;i<5;i++) 25 { 26 chess[a+r[i]][b+c[i]]=!chess[a+r[i]][b+c[i]]; 27 } 28 return; 29 } 30 void dfs(int a,int b,int deep) 31 { 32 if(deep==step) 33 { 34 flag=ac(); 35 return; 36 } 37 if(flag||a==5) 38 return; 39 q(a,b); 40 if(b<4) 41 dfs(a,b+1,deep+1); 42 else 43 dfs(a+1,1,deep+1); 44 q(a,b); 45 if(b<4) 46 dfs(a,b+1,deep); 47 else 48 dfs(a+1,1,deep); 49 return; 50 } 51 main() 52 { 53 int i,j; 54 char a; 55 for(i=1;i<5;i++) 56 { 57 for(j=1;j<5;j++) 58 { 59 cin>>a; 60 if(a==‘b‘) 61 chess[i][j]=1; 62 } 63 } 64 for(step=0;step<=16;step++) 65 { 66 dfs(1,1,0); 67 if(flag) break; 68 } 69 if(flag) 70 printf("%d\n",step); 71 else 72 printf("Impossible\n"); 73 }
标签:
原文地址:http://www.cnblogs.com/CrazyBaby/p/5487459.html