Poj 1222 extended lights out (Gaussian elimination element)

Source: Internet
Author: User

Http://poj.oRG/problem? ID = 1222


First paste a link

Http: // blog.Csdn.net/u013081425/artiCLE/details/24248247

Enumerate the status of the first row and test it. If the last row is 0, this solution is feasible.


Another method is Gaussian elimination.

Reprinted analysis: the name of this game is lights out. There are mxn buttons on a board, and buttons are also lights. Each time you press a button, the button and its top, bottom, and left adjacent buttons change their respective bright and off states at the same time. To give you an initial state, please provide a way to press some buttons to turn off all the lights.

This game has some tips:
1. In the order of buttons.
2. A button can be pressed once at most. Because the second operation offset the first operation, it means that no operation is performed.

This problem can be transformed into a mathematical problem.
The layout of a lamp can be regarded as a matrix of 0 and 1. Take 3x3 as an example:
0 1 0
1 1 0
0 1 1
Indicates a layout. 0 indicates that the light is off, and 1 indicates that the light is on.
Each time you press the button (poj1222) or call a dormitory to turn off the light (0998), you can see the following matrix on the original matrix (mod 2 plus, that is, bitwise OR:
0 1 0
1 1 1
0 1 0
The value 1 in the preceding matrix indicates the range to which the button is applied when the button in Column 2nd in the row 2nd is pressed. If you press the button in the upper left corner:
1 1 0
1 0 0
0 0 0

We record l as the original layout matrix to be solved. A (I, j) indicates the range matrix used when the button in column J of row I is pressed. In the preceding example,
L =
0 1 0
1 1 0
0 1 1

A (1, 1) =
1 1 0
1 0 0
0 0 0

A (2, 2) =
0 1 0
1 1 1
0 1 0

If X (I, j) indicates whether the button in column J of row I needs to be pressed to bring l back to the all-out state. 0 indicates not to press, 1 indicates to press. Then, this game is transformed into solving the following equation:
L + x () * a () + x) * A (2, 1) +... + X (3, 3) * A (3, 3) = 0

X (I, j) is unknown. 0 on the right of the equation indicates the zero matrix, indicating the State in which the equation is completely destroyed. The intuitive understanding is: the original l state, after several a (I, j) transformations, is finally changed to 0: All off state.
Because it is a matrix of 0 and 1, the above equation can also be written:
X () * a () + x () *) +... + X (3, 3) * A (3, 3) = L

This is a matrix equation. The two matrices are equal. A sufficient condition is that each element in the matrix is equal. By expanding the above equation, the system is transformed into a system of 9 yuan and 1 equations:

Simply remember: AA * xx = ll

This equation has a unique solution:
X (1, 1) x (1, 2) x (1, 3)
X (2, 1) x (2, 2) x (2, 3)
X (3, 1) x (3, 2) x (3, 3)
=
1 1 1
0 0 0
0 0 1

That is to say, by pressing the three buttons in the first line and the button in the lower right corner

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

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.