poj:1222-extended LIGHTS out (matrix inversion) __ reversal

Source: Internet
Author: User
Tags first row time limit
EXTENDED LIGHTS out

Time limit:1000ms Memory limit:10000k
Total submissions:12956 accepted:8186 Description

In an extended version of the game Lights out, are 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, which button and each of it (up to four) neighbors above, below, right and left, has the state O F its light reversed. (If on, the light are turned off; if out, the light is turned on.) Buttons in the "corners change" state of 3 Buttons; Buttons on a edge change the state of 4 buttons and other buttons to the state of 5. For example, if the buttons marked X on the left below were to is pressed,the display would the on the rig Ht.

The aim of the "game is", starting from any initial set of lights on "display, to press buttons to get" 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 into the left display results in the right display. Note This buttons in row 2 column 3 and row 2 column 5 both the "State of the" button in row 2 column 4,so that, In the end, the IT 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 "the" the "the" the " Pressed more than once.
3. As illustrated in the second diagram, all the lights in the the "the" May is turned off, by pressing the Correspon Ding buttons in the second row. By repeating this process into each row, all the lights in the the "the" and "I"
four rows is turned out. Similarly, by pressing buttons in columns 2, 3, all lights in the the ' the ' the ' I 5 columns May is turned off.
Write A program to solve the puzzle. Input

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

For each puzzle, the output consists's a line with the string: "Puzzle #m", where M was the index of the puzzle in the INP UT file. Following that line, are 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 are not press Ed. There should be exactly one spaces between each 0 or 1 in the output puzzle-like display. Sample Input

2
0 1 1 0 1 0
1 0 0 1 1 1
0 0 1 0 0 1
1 0 0 1 0 1
0 1 1 1 0 0
0 0 1 0 1 0
1 0 1 0 1 1
0 0 1 0 1 1
1 0 1 1 0 0
0 1 0 1 0 0 Sample Output

Puzzle #1
1 0 1 0 0 1
1 1 0 1 0 1
0 0 1 0 1 1
1 0 0 1 0 0
0 1 0 0 0 0
Puzzle #2
1 0 0 1 1 1
1 1 0 0 0 0
0 0 0 1 0 0
1 1 0 1 0 1
1 0 1 1 0 1

Problem Solving experience: give you a five-element six-column matrix, you can select one of the elements for 0-1 inversion, if the current element is reversed and the element adjacent to the four direction (up and down) of the elements will also be reversed. Now you need to output a reversal scheme to turn the original matrix into a full 0 matrix. is a matrix inversion problem that can be looked at, and the same matrix inversion problem has been mentioned above.

#include <algorithm> #include <stdio.h> #include <climits> #include <cstring> using namespace
Std
const int MAXN = 10;
const int r = 5;

const int c = 6;
int FLIP[MAXN][MAXN],OPT[MAXN][MAXN],TILE[MAXN][MAXN];

int dir[5][2] = {1,0,0,1,0,0,-1,0,0,-1};

void Init () {for (int i=0;i<r;i++) for (int j=0;j<c;j++) scanf ("%d", &tile[i][j]);
    BOOL Check (int x,int y) {//check for out of matrix range if (x < 0 | | Y < 0 | | | x >= r | | y >= c) return true;
return false;
    BOOL Get (int x,int y) {//check whether the position of a row on the same column needs to be reversed int cnt = tile[x][y];
        for (int i=0;i<5;i++) {int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if (check (XX,YY)) continue;
    CNT + + FLIP[XX][YY];
    } if (cnt%2) return true;
return false;
                BOOL Cal () {for (int i=1;i<r;i++) {for (int j=0;j<c;j++) {if (i-1,j)) {
            FLIP[I][J] = 1;

 }
        }
    }   for (int i=0;i<c;i++) {if (r-1,i)] return true;
return false;
        } void Solve () {for (int i=0;i< (1<<6); i++) {memset (flip,0,sizeof (flip));
        for (int j=0;j<c;j++) {//enum first row if (1 & (I >> J)) flip[0][j] = 1;
        The IF (CAL ())//downward inversion continue;
    memcpy (Flip) (opt,flip,sizeof);//if fit, save break;
    } void Print (int t) {printf ("puzzle #%d\n", t); for (int i=0;i<r;i++) for (int j=0;j<c;j++) {printf ("%d%c", opt[i][j],j = = c-1?)
        ' \ n ': ');
    int main () {int t,t = 1;
    scanf ("%d", &t);
        while (t--) {init ();
        Solve ();
    Print (t++);
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.