1 #include <iostream> 2 using namespace std; 3 4 int _m[5][6]; 5 int _p[5][6]; 6 int tem[5][6]; 7 int dir[4][2] = {1,0,-1,0,0,1,0,-1}; 8 bool boo; 9 void DFS(int r,int c); 10 11 int main() 12 { 13 //freopen("acm.acm","r",stdin); 14 int test; 15 int i; 16 int j; 17 int time = 0; 18 cin>>test; 19 while(test --) 20 { 21 boo = false; 22 memset(_p,0,sizeof(_p)); 23 24 for(i = 0; i < 5; ++ i) 25 { 26 for(j = 0; j < 6; ++ j) 27 { 28 cin>>_m[i][j]; 29 } 30 } 31 32 DFS(0,0); 33 cout<<"PUZZLE #"<<++ time<<endl; 34 if(boo) 35 for(i = 0; i < 5; ++ i) 36 { 37 for(j = 0; j < 6; ++ j) 38 { 39 cout<<_p[i][j]<<" "; 40 } 41 cout<<endl; 42 } 43 } 44 45 } 46 47 void DFS(int r,int c) 48 { 49 int i; 50 int j; 51 int tem_a; 52 int tem_b; 53 int sum = 0; 54 if(r == 6) 55 { 56 boo = true; 57 return; 58 } 59 60 61 if(r == 5) 62 { 63 c = 6; 64 } 65 66 if(c == 6) 67 { 68 if(r >= 1) 69 { 70 for(i = 0; i < 6; ++ i) 71 { 72 sum = 0; 73 for(j = 0; j < 4; ++ j) 74 { 75 tem_a = r-1+dir[j][0]; 76 tem_b = i + dir[j][1]; 77 if(tem_a >= 0 && tem_a < 5 && tem_b >= 0 && tem_b < 6) 78 { 79 sum += _p[tem_a][tem_b]; 80 } 81 } 82 sum += _p[r-1][i]; 83 if(sum % 2 != 0) 84 { 85 if(_m[r-1][i] == 0) 86 { 87 return; 88 } 89 } 90 if(sum % 2 == 0) 91 { 92 if(_m[r-1][i] == 1) 93 { 94 return; 95 } 96 } 97 } 98 99 } 100 DFS(r+1,0); 101 if(boo) 102 return; 103 } 104 else 105 { 106 _p[r][c] = 1; 107 DFS(r,c+1); 108 if(boo) 109 return; 110 _p[r][c] = 0; 111 DFS(r,c+1); 112 } 113 }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com