Poj1222 extended lights out, poj1222
EXTENDED LIGHTS OUT
Question: to give a 01 matrix of 5*6, an operation on a position (0-> 1 turn on the light or 1-> 0 turn off the light) will affect (including yourself) the surrounding light status is reversed. Operation matrix for turning off all the lights (1 indicates that the lights at the position are operated)
Tip: 01 matrix. If the question is "Operation twice", it is equivalent to "no operation", but it also implies that this is an exclusive or operation (I .e., mod2 ); the 0th matrix is obtained by adding an operation matrix to the 01 matrix. The matrix is the same as the original one. (operation matrix: for example, (0, 0) the operation is equivalent to adding a matrix with only three ones in the upper left corner, But converting this matrix into a vector form with a dimension of 30, there are 30-stack lamps, so the following is a 30*30 matrix. This is the basis of the Gaussian elimination matrix equation;
Gaussian elimination method: the most important thing is to think of creating a 30*30 matrix, in column I numbers (column numbers 0 ~ 29 indicates the lamp number.) 1 indicates that the lamp of the I-th lamp affects the lamp of the corresponding line number of 1 and is stored in the augmented matrix;
The Augmented Matrix is a matrix that abstracts the coefficients and results containing only the equations (simplified writing ). In this way, the final state of the lamp I is a [I] [30] = Σ (a [I] [j] * x [j]) (0 <= j <30 ). This is why the row vector operation is performed after being converted into an upper triangle; (a [I] [j] & x [j] indicates that the operation of the j lamp affects the I lamp, and the j lamp is on. Another premise is that a [I] [I] = 1, So x [I] = a [I] [var])
Note: When the Gaussian elimination element column equation is transferred to the matrix, it is best to directly look at the correspondence between each coefficient and the variable, rather than using the row vector and column vector.
# Include <iostream> # include <cstdio> # include <cstring> # include <string. h> # include <algorithm> # include <map> # include <queue> # include <vector> # include <cmath> # include <stdlib. h> # include <time. h> using namespace std; # define rep0 (I, l, r) for (int I = (l); I <(r); I ++) # define rep1 (I, l, r) for (int I = (l); I <= (r); I ++) # define rep_0 (I, r, l) for (int I = (r); I> (l); I --) # define rep_1 (I, r, l) for (int I = (r ); i> = (l); I --) # def Ine MS0 (a) memset (a, 0, sizeof (a) # define MS1 (a) memset (a,-1, sizeof ()) int dir [2] [4] = {0, 0,-1}, {,-}; int a [35] [35]; int equ, var; int x [35]; int Guass () {int I, j, k, free_var = 0; rep0 (I, 0, equ) {int mx = I; rep0 (j, I + 1, equ) if (abs (a [j] [I])> abs (a [mx] [I]) mx = j; if (a [mx] [I] = 0) {free_var ++; continue;} if (mx! = I) rep1 (k, I, var) swap (a [I] [k], a [mx] [k]); rep0 (j, I + 1, equ) {if (a [j] [I]) {// The j lamp may also affect the I lamp. rep1 (k, I, var) a [j] [k] ^ = a [I] [k] ;}} if (free_var! = 0) return free_var; rep_1 (I, var-1, 0) {x [I] = a [I] [var]; rep0 (j, I + 1, equ) x [I] ^ = (a [I] [j] & x [j]); // The lamp number j affects the lamp number I, at the same time, the j lamp will also be on.} Void init () {int I, j, k; rep0 (I,) rep0 (j,) {int x = I * 6 + j; a [x] [x] = 1; rep0 (k, 0,4) {int nx = I + dir [0] [k], ny = j + dir [1] [k]; int pos = nx * 6 + ny; if (nx <0 | nx> = 5 | ny <0 | ny> = 6) continue; a [pos] [x] = 1; // It is a symmetric matrix, because the influences are mutual, but the idea of writing [x] [pos] is different.} int main () {int T, kase = 1, I; cin> T; while (T --) {MS0 (x); MS0 (a); rep0 (I, 0, 30) scanf ("% d ", & a [I] [30]); init (); equ = var = 30; Guass (); Printf ("PUZZLE # % d \ n", kase ++); rep0 (I, 0, 30) printf ("% d % c", x [I], (I + 1) % 6? '': '\ N');} return 0 ;}View Code