POJ 1222 EXTENDED LIGHTS out (Gaussian elimination)

Source: Internet
Author: User
Tags gcd

Transmission Door

EXTENDED LIGHTS out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8857 Accepted: 5740

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
Main topic:

There is a 5*6 matrix, each square represents a lamp, each operation of a lamp, around the upper and lower left and right four lights to change the light, from the light to turn off, how to make the lamp all out, and output the operation matrix (1 means press, 0 means no press)


Problem Solving Ideas:

In fact, this topic is similar to the previous topic (this topic is more simple than the previous one), because finally let us output the matrix, and according to the meaning of the topic, there is no free variable, that is, the matrix is directly determined, the only solution, and then we construct a matrix, with the same structure, Then directly set the template on the line.


My Code:

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath >using namespace Std;const int maxn = 1e2+5;int equ, var;///equ equation var variable int a[maxn][maxn];///augmented matrix int x[maxn];///solution number B    Ool free_x[maxn];///judge is not a free variable int free_num;///The number of free arguments inline int GCD (int m, int n) {if (n = = 0) return m; Return GCD (n, m%n);} inline int LCM (int a, int b) {return a/gcd (b) *b;}    int Gauss () {int max_r;///the current column with the largest absolute value of the existing row///col: handles the current column int row = 0;    int free_x_num;    int free_index;        for (int col=0; row<equ&&col<var; row++,col++) {max_r = row;        for (int i=row+1; i<equ; i++) if (ABS (A[i][col]) > abs (A[max_r][col])) Max_r = i;        if (max_r! = row) for (int i=0; i<var+1; i++) Swap (A[row][i], a[max_r][i]);            if (a[row][col] = = 0) {row--;        Continue            } for (int i=row+1; i<equ; i++) {if (A[i][col]) {for (int j=col; j<var+1; j + +) A[i][j] ^= a[row][j];    }}} for (int i=row; i<equ; i++) if (A[i][var]) return-1;///no solution if (Row < Var)            {for (int i=row-1; i>=0; i--) {free_x_num = 0; for (int j=0; j<var; J + +) if (A[i][j] && free_x[j]) {Free_x_num                    ++;                Free_index = j;            } if (Free_x_num > 1) continue;            int tmp = A[i][var];            for (int j=0; j<var; J + +) if (A[i][j] && j!=free_index) tmp-= a[i][j]*x[j];            X[free_index] = tmp/a[i][free_index];///to find the variable. Free_x[free_index] = 0;        The variable is deterministic.        } return var-row;///the number of free arguments} for (int i=var-1; i>=0; i--) {int tmp = A[i][var];         for (int j=i+1; j<var; j + +)   if (A[i][j]) tmp ^= A[I][J]*X[J]; if (Tmp%a[i][i]) return-2;        Indicates that there is a floating-point number solution, but no integer solution.    X[i] = Tmp/a[i][i];    } return 0;///Unique solution}int main () {int T;    cin>>t;        for (int cas=1; cas<=t; cas++) {memset (A, 0, sizeof (a));        memset (x, 0, sizeof (x));        Equ = var = 30;            for (int i=0; i<var; i++) {int ta = i%6, TB = I/6;            A[i][i] = 1;            if (Ta > 0) a[i][i-1] = 1;            if (Ta < 5) a[i][i+1] = 1;            if (tb > 0) a[i][i-6] = 1;        if (TB < 5) a[i][i+6] = 1;        } for (int i=0; i<equ; i++) cin>>a[i][var];        int k = Gauss ();        printf ("PUZZLE #%d\n", CAs);            for (int i=0; i<var; i++) {if (i%6 = = 5) cout<<x[i]<<endl;        else cout<<x[i]<< ""; }} return 0;}




POJ 1222 EXTENDED LIGHTS out (Gaussian elimination)

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.