Http://poj.org/problem? Id = 1222
Similar to poj 1753, 1753 is the number of steps for full 0 or full 1. This is a solution for full 0.
At that time, I changed the code by 1753. The number of enumeration steps is up to 30 steps. In this case, the total number of States is 2 ^ 30...
Enumerate the status of the first row, which is 2 ^ 6 in total. If the first row is determined, you can determine the remaining rows and check whether all the last rows are 0.
Code:
# Include <cstdio>
# Include <cstring>
Int map [5] [6], ANS [5] [6];
Int Tur [5] [2] = {0, 0, 0, 1, 0,-1, 1, 0,-1, 0 };
Int flag;
Void Tur (int x, int y ){
For (INT I = 0; I <5; I ++ ){
Int Tx = x + Tur [I] [0];
Int ty = Y + Tur [I] [1];
If (TX> = 0 & ty> = 0 & TX <5 & ty <6)
Map [TX] [ty] ^ = 1;
}
}
Void work () {// determine the remaining ans based on the status of the first line
For (INT I = 1; I <5; I ++)
For (Int J = 0; j <6; j ++)
If (Map [I-1] [J]) {
Tur (I, j );
Ans [I] [J] = 1;
}
}
Void back (){
For (INT I = 1; I <5; I ++)
For (Int J = 0; j <6; j ++)
If (ANS [I] [J]) {
Tur (I, j );
Ans [I] [J] = 0;
}
}
Void test (){
Flag = 1;
For (INT I = 0; I <6; I ++)
If (Map [4] [I] = 1) Flag = 0;
}
Void print (){
Int I, J;
For (I = 0; I <5; I ++ ){
For (j = 0; j <5; j ++)
Printf ("% d", ANS [I] [J]);
Printf ("% d \ n", ANS [I] [J]);
}
}
Void DFS (INT y) {// enumerate the 64 States in the first row
If (FLAG) return;
If (y = 6 ){
Work ();
Test ();
If (FLAG) print ();
Else back ();
}
Else {
Tur (0, y );
Ans [0] [Y] = 1;
DFS (Y + 1 );
Tur (0, y );
Ans [0] [Y] = 0;
DFS (Y + 1 );
}
}
Int main (){
Int T = 0, I, j, N;
Scanf ("% d", & N );
While (n --){
Memset (ANS, 0, sizeof (ANS ));
Flag = 0;
For (I = 0; I <5; I ++)
For (j = 0; j <6; j ++)
Scanf ("% d", & map [I] [J]);
Printf ("puzzle # % d \ n", ++ t );
DFS (0 );
}
Return 0 ;}