POJ EXTENDED LIGHTS out 1222 "Gaussian elimination element"

Source: Internet
Author: User

Language:DefaultEXTENDED LIGHTS out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7672 Accepted: 4996

Description

In an extended version of the game Lights out, was a puzzle with 5 rows of 6 buttons each (the actual puzzle have 5 rows of 5 buttons each). Each button is a light. When a button is pressed, then button and each of it (up to four) neighbors above, below, right and left, have the state O F its light reversed. (If on, the light was turned off; if off, the light was turned on.) Buttons in the corners and the state of 3 Buttons; Buttons on a edge change the state of 4 buttons and other buttons the state of 5. For example, if the buttons marked X in the left below were to is pressed,the display would change to the image on the rig Ht.

The aim of the game is, starting from any initial set of lights on the display, to press buttons to get the display to A state where all lights is off. When adjacent buttons is pressed, the action of one button can undo the effect of another. For instance, in the display below, pressing buttons marked X on the left display results in the right display. Note that the buttons in row 2 column 3 and row 2 column 5 both change the state of the button in row 2 column 4,so that, In the end, it is unchanged.

Note:
1. It does not matter what order the buttons is pressed.
2. If A button is pressed a second time, it exactly cancels the effect of the first press, so no button ever need be press Ed more than once.
3. As illustrated in the second diagram, all the lights in the first row is turned off, by pressing the corresponding Buttons in the second row. By repeating this process in each row, all the lights in the first
Four rows is turned out. Similarly, by pressing buttons in columns 2, 3?, all lights in the first 5 columns could be turned off.
Write a program to solve the puzzle.

Input

The first line of the input is a positive integer n which are the number of puzzles that follow. Each puzzle is five lines, each of the which have six 0 or 1 separated by one or more spaces. A 0 indicates that's the light is off and while a 1 indicates that's on initially.

Output

For each puzzle, the output consists of a line with the string: "Puzzle #m", where M was the index of the puzzle in the INP UT file. Following a puzzle-like display (in the same format as the input). In this case, 1 ' s indicate buttons that must is pressed to solve the puzzle, while 0 indicate buttons, which is not press Ed. There should is exactly one space between each 0 or 1 in the output puzzle-like display.

Sample Input

20 1 1 0 1 01 0 0 1 1 10 0 1 0 0 11 0 0 1 0 10 1 1 1 0 00 0 1 0 1 01 0 1 0 1 10 0 1 0 1 11 0 1 1 0 00 1 0 1 0 0

Sample Output

PUZZLE #11 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 1 11 1 0 0 0 00 0 0 1 0 01 1 1 0 1

Source

Greater New York 2002

The classical problem of Gauss elimination element

Test instructions: give you a 5*6 matrix of lights, 0 for the off, 1 for the open. If you turn on a light, it changes the state of the lights up and down. Ask how to press these lights, will turn off all the lights.

Analysis:

The first Gaussian elimination!!! The state can be considered as a matrix multiplication, because it is 30 lights, so the state of each lamp, all the state of the combination is a 30*30 matrix, and each lamp state is set to an unknown amount of 30 unknowns, and then construct 30 equations. It is converted to the Gaussian elimination problem.

The given end state is a B-matrix. The initial state is a matrix, and what we are asking is that the linear combination of a is equal to the condition of B. How do I find this matrix?

Because the given end state is a 0 matrix. We consider the 0 matrix as a b matrix, and the given initial state is considered a matrix.

And what we're asking for is solving X.

Suppose A is a 3*3 matrix:

0 1 0
a=1 1 0
0 1 1

We need to find out what the scope of each lamp is when we press it.

1 1 0
A (+) = 1 0 0
0 0 0

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

We find out the impact of all the lights, x indicates whether the lamp is pressed.

We consider A + x (*a) (+) + x (*a) + x (1,3) *a (1,3) + x (2,1) *a (2,1) + ... + x (3,3) *a (3,3) = 0.

Because it is an XOR operation. We can add a matrix to both sides. A^a is the 0 matrix. So the equation becomes:

X (*a) + x (*a) + x ( 1,3) *a (1,3) + x (2,1) *a (2,1) + ... + x (3,3) *a (3,3) = A.

Then the augmented matrix is asked. a| A. Then it is possible to use Gauss elimination to find out X.

#include <stdio.h> #include <math.h> #include <vector> #include <queue> #include <string> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using namespace Std;const int Maxn=40;int Equ,var; Equ an equation, var variable int X[MAXN];    Solution set int a[maxn][maxn];//augmented matrix int Gauss () {int max_r;    int col=0;    int i,j,k;        for (int k=0;col<=var&&k<equ;k++,col++) {max_r=k;        for (int i=k+1;i<equ;i++)//Find maximum variable if (ABS (A[i][col]) >abs (A[max_r][col])) max_r=i;        if (max_r!=k)//swap the maximum variable where the row is with the current line for (int i=col;i<=var;i++) swap (a[k][i],a[max_r][i]);        if (!a[k][col]) {//current variable is 0, return the previous row k--;continue;                } for (int i=k+1;i<equ;i++) if (A[i][col])//current variable is not 0 for (int j=col;j<=var;j++)    A[I][J]^=A[K][J];        } for (int i=var-1;i>=0;i--) {X[i]=a[i][var]; for (int j=i+1;j<var;j++) x[i]^= (A[i][j]&&x[j]); } return 0;} void init ()//Initialize a matrix.    For the function range of the lamp {memset (a,0,sizeof (a));    var=30;    equ=30;            for (int i=0;i<5;i++) {for (int j=0;j<6;j++) {int t=i*6+j;            A[t][t]=1;            if (i>0) a[(i-1) *6+j][t]=1;            if (i<4) a[(i+1) *6+j][t]=1;            if (j>0) a[t][i*6+j-1]=1;        if (j<5) a[t][i*6+j+1]=1;    }}}int Main () {int T;    scanf ("%d", &t);    int xp=1;        while (t--) {init ();        for (int i=0;i<30;i++) scanf ("%d", &a[i][30]);        Gauss ();        printf ("PUZZLE #%d\n", xp++);                for (int i=0;i<30;i++) {if ((i+1)%6==0) {printf ("%d\n", X[i]);            Continue        } printf ("%d", x[i]); }} return 0;}


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

POJ EXTENDED LIGHTS out 1222 "Gaussian elimination element"

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.