Enumeration algorithm--Lights Out problem

Source: Internet
Author: User

Enumeration Method Exercises:Title:

There is a matrix of buttons, each of which has 6 buttons and a total of 5 rows. There is a light on the position of each button. When a button is pressed, the lights of the button and the surrounding position (top, bottom, left, right) are changed once. That is, if the light is lit, it will be extinguished, if the lamp is turned off, it will be lit. The button on the corner of the matrix changes the state of the 3 lights; the button on the edge of the matrix changes the state of the 4 lights; the other buttons change the state of the 5 lights.


in the left matrix, the button labeled X is pressed, The matrix on the right indicates a change in the lamp state. Set an initial state for each lamp in the matrix. Please press the button until each one is extinguished. When multiple buttons adjacent to a lamp are pressed, one action cancels the result of another operation. In the 2nd row, the buttons of the 3rd and 5 columns are pressed, so the status of the lights in row 2nd and 4th are not changed.



Span style= "color: #464646; Font-family:simsun; font-size:14px; White-space:pre; " > Please write a program that determines which buttons you need to press to make all the lights go off. According to the above rules, we know 1) the 2nd time when you press the same button, the result of the 1th press will be offset. Therefore, each button can only be pressed once, 2) each button is pressed in the order of the final result has no effect, 3) on the 1th line in the light of each lamp, press the 2nd line corresponding to the button, you will be able to extinguish the 1th line of all lights. This repetition can extinguish all lights in lines 1th, 2, 3 and 4. Similarly, press 1th, 2, 3, 4, 5 columns of the button, you can extinguish the first 5 columns of lights.


inputThe first line is a positive integer n, which indicates the number of cases that need to be resolved. Each case consists of 5 lines, each of which consists of 6 digits. These numbers are separated by a space, which can be 0 or 1. 0 indicates that the initial state of the lamp is off, and 1 indicates that the initial state of the lamp is lit. Outputfor each case, output a line first, the output string "PUZZLE #m", where M is the ordinal of the case. Then output 5 lines according to the input format of the case, where 1 indicates that the corresponding button needs to be pressed, and 0 means that the corresponding button is not required. Each number is separated by a space. Sample Input:
2 0 1 1 0 1 01 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 0PUZZLE #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 

1 //Program Implementation2#include <iostream>3 using namespacestd;4 intpuzzle[6][8], press[6][8];//the puzzle array indicates the light off state, 1 is on, 0 means5                                     //The press array indicates: "Do I need to light or turn off the light at the specified position", 1 means yes, 0 means no6 BOOLGuess ();//declaring the Guess function7 voidEnumeration ();//declaring the enumeration function8 intMain () {9     intN;TenCIN >>N; One     if(n = =0) Acout <<"error!"<<Endl; -     //This procedure is optional -     /*for (int i = 0; i < 6; i++) the         { - press[i][0] = 0; - press[i][7] = 0; - puzzle[i][0] = 0; +         } - for (int i = 0; i < 8; i++) +         { A Press[0][i] = 0; at Puzzle[0][i] = 0; -         }*/ -      for(inti =0; I < n; i++) -     { -          for(intR =1; R <6; r++) -              for(intc =1; C <7; C++) in             { -CIN >>Puzzle[r][c]; to             } +  -         //calling Functions the enumeration (); *cout <<"puzzle#"<< i+1<<Endl; $          for(intR =1; R <6; r++)Panax Notoginseng         { -              for(intc =1; C <7; C++) the             { +cout << Press[r][c] <<" "; A             } thecout <<Endl; +         } -     } $ } $ //Judging function - BOOLGuess () - { the      for(intR =1; R <5; r++) -          for(intc =1; C <7; C++)WuyiPress[r +1][C] = (Puzzle[r][c] + press[r][c] + thePress[r][c +1] + press[r][c-1] + press[r-1][C])%2; -      Wu      for(intc =1; C <7; C++) -         if(puzzle[5][C]! = (press[5][C] + press[5][c +1] + Aboutpress[5][c-1] + press[4][C])%2) $         { -             return false; -         } -              A     return true; + } the //Enumeration Functions - voidenumeration () $ { the     intC; the      for(c =0; c < C; C++) the     { thepress[1][C] =0; -     } in      while(guess () = =false) the     { thepress[1][1]++; Aboutc =1; the          while(press[1][c]>1)  the         { thepress[1][C] =0; +C++; -press[1][c]++; the         }Bayi     } the}



Enumeration algorithm--Lights Out problem

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.