Poj 1222 extended lights out Gaussian elimination Element

Source: Internet
Author: User
Click Open link extended lights out
Time limit:1000 ms   Memory limit:10000 K
Total submissions:6492   Accepted:4267

Description

In an extended version of the game lights out, is a puzzle with 5 rows of 6 buttons each (the actual puzzle has 5 rows of 5 buttons each ). each button has a light. when a button is pressed, that button and each of its (up to four) neighbors above, below, right and left, has the state of its light reversed. (If on, the light is turned off; if off, the light is turned on .) buttons in the corners change the state of 3 buttons; buttons on an edge change the state of 4 buttons and other buttons change the state of 5. for example, if the buttons marked X on the left below were to be pressed, the display wocould change to the image on the right.

The aim of the game is, starting from any initial set of lights on in the display, to press buttons to get the display to a state where all lights are off. when adjacent buttons are pressed, the action of one button can undo the effect of another. for instance, in the display below, pressing buttons marked X in 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, its state is unchanged.

Note:
1. It does not matter what order the buttons are 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 pressed more than once.
3. as specified strated In the second digoal, all the lights in the first row may be 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 may be turned out. Similarly, by pressing buttons in columns 2, 3 ?, All lights in the first 5 columns may be turned off.
Write a program to solve the puzzle.

Input

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

Output

For each puzzle, the output consists of a line with the string: "puzzle # m", where M is the index of the puzzle in the input file. following that line, is a puzzle-like display (in the same format as the input ). in this case, 1's indicate buttons that must be pressed to solve the puzzle, while 0 indicate buttons, which are not pressed. there shoshould be 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 11 1 0 1 0 10 0 1 0 1 11 0 0 1 0 00 1 0 0 0 0PUZZLE #21 0 0 1 1 11 1 0 0 0 00 0 0 1 0 01 1 0 1 0 11 0 1 1 0 1

Source

Greater New York 2002
A 5*6 matrix is provided. Each matrix unit has a light. you can press the switch to change the status of the light and the lights around it. This gives you an initial status, so that you can press it to turn off all the lights. You can define 30 unknown numbers: x0, x1, x2, x3.... x29, which indicates whether the switch of each lamp is pressed. Each lamp can have an equation. For the lamp at the position (I, j), you can list it: X (I * 6 + J) + X (I * 6 + J + 1) + X (I * 6 + J-1) + x (I + 1) * 6 + J) + x (I-1) * 6 + J) = A % 2 where A is the initial switch state given in the question. 30 such equations can be listed, and the answer can be obtained by Gaussian elimination.
//140K0MS#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>#include<map>#include<stack>#include<queue>#include<vector>#pragma comment(linker, "/STACK:1024000000");#define M 100007#define inf 0x3f3f3f3f#define ll long long#define mod 1000000009#define eps (1e-8)using namespace std;int matrix[37][37],id[37];int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};void Guess(int n,int m){    int i=0,j=0;    while(i<n&&j<m)    {        int max_i=i;        for(int k=i+1;k<n;k++)            if(matrix[k][j]==1)            {                max_i=k;                break;            }        if(matrix[max_i][j])        {            if(max_i!=i)                for(int k=0;k<m;k++)                    swap(matrix[max_i][k],matrix[i][k]);            for(int u=0;u<n;u++)                if(i!=u&&matrix[u][j])                    for(int k=j;k<m;k++)                        matrix[u][k]=matrix[u][k]^matrix[i][k];            i++;        }        j++;    }}int main(){    int n,cas=1;    scanf("%d",&n);    while(n--)    {        memset(matrix,0,sizeof(matrix));        memset(id,0,sizeof(id));        for(int i=0;i<5;i++)            for(int j=0;j<6;j++)            {                scanf("%d",&id[i*6+j]);                matrix[i*6+j][30]=id[i*6+j];            }        for(int i=0;i<5;i++)            for(int j=0;j<6;j++)            {                matrix[i*6+j][i*6+j]=1;                for(int k=0;k<4;k++)                {                    int x=i+dir[k][0];                    int y=j+dir[k][1];                    if(x>=0&&x<5&&y>=0&&y<6)                        matrix[i*6+j][x*6+y]=1;                }            }        Guess(30,31);        printf("PUZZLE #%d\n",cas++);        for(int i=0;i<5;i++)        {            for(int j=0;j<6;j++)                printf("%d ",matrix[i*6+j][30]);            printf("\n");        }    }}


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.