Poj 1222 & zoj 1354 extended lights out (enumeration)

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.