Algorithm design Lights Out problem (enumeration method)

Source: Internet
Author: User

Title Description:

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.

So in the 5x6 matrix, the X-labeled button in the left-hand matrix is pressed and the right matrix indicates a change in the light 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.

Please write a program to determine which buttons need to be pressed, just so that all the lights are off. According to the above rules, we know

1) The 2nd time you press the same button, the result of the 1th press will be offset. Therefore, each button can only be pressed once;

2) The order of each button being pressed has no effect on the final result;

3) on the 1th row of each lamp lit, press the corresponding button 2nd line, you can extinguish all the lights on line 1th. 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.

Request the sample access, output the desired operation.

Problem Solving Ideas:

For convenience, set a coordinate for each lamp position, get an array of 5x6, but in order to avoid the first row, the last column of the first column needs extra action, we say the array is set to 6x8 's two-dimensional array.

PUZZLE[I][J] Indicates the first initial state of the lamp on row J of Line I, 1 is bright and 0 is extinguished;

PRESS[I][J] Indicates to not press the IJ position of the lamp, 1 is pressed;

If that's the case with 2 of the 30-way situation, too complex, to optimize the algorithm, we found the law

If the position (1,J) light is on, then the value of press[2][j] must be 1, and vice versa, all through the operation, the first line of the lights are all off, and the 3,4,5 row is unaffected, continue the subsequent operation.

The code is as follows:

1#include"stdio.h"2 3 intpuzzle[6][8];4 intpress[6][8]; 5 6 BOOLguess () {7     inti,j;8           for(i=2;i<6; i++){9                for(j=1;j<7; j + +){TenPress[i][j]= (press[i-1][j]+puzzle[i-1][j]+press[i-1][j-1]+press[i-2][j]+press[i-1][j+1])%2; One                   } A              } -           for(j=1; j<=6; j + +){ -             if(press[5][j]!= (puzzle[5][j]+press[5][j-1]+press[5][j+1]+press[4][J])%2) the                return false; -              } -          return true; -     } + voidprocess () { -     intC; +      for(c=1;c<7; C + +) Apress[1][c]=0; at      while(!guess ()) { -press[1][1]++; -C=1; -          while(press[1][c]>1){ -press[1][c]=0; -C++; inpress[1][c]++;  -           } to      } + } -  the intMain () { *     intI=0, j=0; $      for(i=0;i<6; i++)Panax Notoginsengpuzzle[i][0]=puzzle[i][7]=press[i][0]=press[i][7]=0; -      for(j=0;j<8; j + +) thepuzzle[0][j]=puzzle[5][j]=press[0][j]=press[5][j]=0; +          A      for(i=1;i<6; i++) the          for(j=1;j<7; j + +) +scanf"%d",&Puzzle[i][j]);  -      $ process ();  $printf"Press is:\n"); -          -      for(i=1;i<6; i++){  the          for(j=1;j<7; j + +){  -printf"%d", Press[i][j]); Wuyi         }  theprintf"\ n"); -     }  Wu}

SOURCE Download (Baidu Cloud): Link: http://pan.baidu.com/s/1ntOqG7R Password: ewij

Algorithm design Lights Out problem (enumeration method)

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.