Poj 1222 extended lights out (Math _ Gaussian deyuan)

Source: Internet
Author: User

To give a 01 matrix of 5*6, 0 indicates that the light is dark, and 1 indicates that the light is on. Each position in the matrix represents a button. When the button is pressed, the lights around it (up and down) are in the opposite state. Ask how to turn all the lights into a dark one.

Solution: This type of switch problem is a classic Gaussian elimination problem. When doing this, I can think of how to create the matrix, but the solution below does not know how to solve it, the line generation teacher died early.
The final state of each lamp is determined only by itself and the button next to it. There are 30 lamps. The final state of each lamp is 0. If the initial state is 0, the center must be an even number of times; otherwise, it is an odd number of times. Let's set 30 equations. The value modulus 2 on the right side of each equation equals the value corresponding to the matrix. In this way, the values in the matrix are all 0. If some vertices can affect this vertex, then the vertices are marked as 1. In this way, each vertex corresponds to an unknown number, and the last value is 0 or 1.
After the matrix is constructed, you can modify the Gaussian elimination template. Because this question has only 0 and 1 in a special solution, you only need to use a few bits.

Test data:
1
0 1 1 0 1 0
1 0 0 1 1 1
0 0 1 0 0 1
1 0 0 1 0 1
0 1 1 1 0 0
Out:
PUZZLE #1
1 0 1 0 0 1
1 1 0 1 0 1
0 0 1 0 1 1
1 0 0 1 0 0
0 1 0 0 0 0

C producer code:
[Cpp]
<Span style = "color: # ff0000;" >#include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;
# Define MOD 2
# Define MAX 35
 
 
Int n, m, x [MAX];
Int arr [MAX] [MAX];
Int mat [MAX] [MAX];
Int dir [4] [2] = {}, {-}, {}, {0,-1 }};
 
 
Void Initial (){
 
Int I, j, k, row, col;
 
 
N = m = 5*6;
Memset (mat, 0, sizeof (mat ));
For (I = 0; I <5; ++ I)
For (j = 0; j <6; ++ j ){
 
Row = I * 6 + j;
Mat [row] [row] = 1;
Mat [row] [m] = arr [I] [j];
For (k = 0; k <4; ++ k ){
 
Int ii = dir [k] [0] + I;
Int jj = dir [k] [1] + j;
If (ii> = 0 & ii <5
& Jj> = 0 & jj <6)
Mat [row] [ii * 6 + jj] = 1;
}
}
}
Int Gcd (int x, int y ){
 
Int r = x % y;
While (r ){
 
X = y, y = r;
R = x % y;
}
Return y;
}
Int Lcm (int x, int y ){
 
Return x/Gcd (x, y) * y;
}
Void Gauss_AC (){
 
Int I, j, row, max_r, col;
Int ta, tb, temp, LCM;
 
 
Row = col = 0;
For (; row <n & col <m; ++ row, ++ col ){
 
Max_r = row;
For (j = row + 1; j <n; ++ j)
If (mat [j] [col]) {
 
Max_r = j;
Break;
}
If (mat [max_r] [col] = 0 ){
 
Row --;
Continue;
}
 
 
If (row! = Max_r)
For (j = col; j <= m; ++ j)
Swap (mat [row] [j], mat [max_r] [j]);
For (I = row + 1; I <n; ++ I) // the first element of each row is reduced to 0. Therefore, use the row to remove an exclusive or I row.
If (mat [I] [col]) for (j = col; j <= m; ++ j)
Mat [I] [j] ^ = mat [row] [j];
}
 
 
For (I = m-1; I> = 0; -- I ){
 
X [I] = mat [I] [m]; // mat [I] [I] is 1, the sum of mat [I] [m] and other columns must be 0, so we use an exclusive or. If the exclusive or is 1, x [I] is 1.
For (j = I + 1; j <m; ++ j)
X [I] ^ = (mat [I] [j] & x [j]);
}
}
 
 
Int main ()
{
Int I, j, k, t, cas = 0;
 
 
Scanf ("% d", & t );
While (t --){
 
For (I = 0; I <5; ++ I)
For (j = 0; j <6; ++ j)
Scanf ("% d", & arr [I] [j]);
 
 
Initial ();
Gauss_AC ();
Printf ("PUZZLE # % d \ n", ++ cas );
For (I = 0; I <m; ++ I)
Printf ("% d % c", x [I], (I % 6) = 5? '\ N ':'');
}
}
 
</Span>


Author: woshi250hua

Related Article

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.