Enables the L state to be completely destroyed. For arrays with fixed columns, the AA matrix is also determined. Whether a solution exists and whether the solution is unique depends only on the aa matrix. For a unique solution, multiply ll by the inverse matrix of AA. The Gaussian elimination method can be used to calculate the inverse matrix of AA in detail.Because it is a matrix of 0 and 1, the above equation can also be written: Adding an L matrix when the two sides of the 1 formula are the same can be changed X () * a () + x () *) +... + X (3, 3) * A (3, 3) = L A () converts a matrix to a column vector, and l to a column vector, The equations can be established by equating the values at the corresponding positions of Sigma Xi * Ai = Li. X1 * a () 1 + x2 * a () 1 + X3 * a () 1 + ............ X30 * a (30,30) 1 = L1; mod 2 X1 * a () 2 + x2 * a () 2 + X3 * a () 2 + ............ X30 * a (30,30) 2 = L2; mod 2 X1 * a () 3 + x2 * a () 3 + X3 * a () 3 + ............ X30 * a (30,30) 3 = L3 mod 2 ....... ....... ....... X1 * a () 30 + x2 * a () 30 + X3 * a () 30 + ............ X30 * a (30,30) 30 = L30; mod 2 Where a (I, j) k indicates the K element in column vector * Indicates the dot multiplication, and XI is equal to (1, 0) + modulo 2 addition. Therefore, ^ exclusive or operation can be used to describe Gaussian elements.
First, we can regard the matrix composed of 6*5 lamps as a 1*30 vector. Then, for each switch I, we also construct a 1*30 vector d (I). One switch controls up to five lights, and the switch status changes to 1, if it is not changed, it is 0. In this way, we can make up 30 toggle vectors into a matrix of 30*30. We are constructing a 30*1 vector ans, that is, the result we requested, ANS [I] is 1, indicating that you need to press the I switch, 0 indicates that no. In this way, ANS * D = a (mod 2) and (D is a 30*30 matrix) are converted to the problem of solving the equation. As for solving the equation, there is nothing to say, that is, the method described in the line generation can be used. Because the modulo 2 is required, we can directly use the exclusive or operation of the computer.
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-12#define PI acos(-1.0)#define C 240#define S 20using namespace std;int a[35][35];int x[35];int equ,var;void init(){memset(x,0,sizeof(x));memset(a,0,sizeof(a));}void Gauss(){int row,col,i,j;row = col = 0;while(row < equ && col < var){for(i = row; i < equ; i++)if(a[i][col] != 0)break;if(i != row){for(j = col; j <= var; j++)swap(a[row][j],a[i][j]);}if(a[row][col] == 0){col++;continue;}for(int i = row+1; i < equ; i++){if(a[i][col] == 0) continue;for(int j = col; j <= var; j++)a[i][j] ^= a[row][j];}row++;col++;}for(i = var-1; i >= 0; i--){x[i] = a[i][var];for(j = i+1; j < var; j++)x[i] ^= (a[i][j]&&x[j]);}}int main(){int test;int num;scanf("%d",&test);for(int item = 1; item <= test; item++){init();for(int i = 0; i < 30; i++)scanf("%d",&a[i][30]);for(int i = 0; i < 5; i++){for(int j = 0; j < 6; j++){int num = i*6+j;a[num][num] = 1;if(i >= 1)a[num-6][num] = 1;if(i <= 3)a[num+6][num] = 1;if(j - 1 >= 0)a[num-1][num] = 1;if(j + 1 <= 5)a[num+1][num] = 1;}}equ = 30;var = 30;Gauss();printf("PUZZLE #%d\n",item);for(int i = 0; i < 30; i++){if(i % 6 == 5){printf("%d",x[i]);printf("\n");}elseprintf("%d ",x[i]);}}return 0;} |