acm pku 1222 Extended lights out的算法分析

EXTENDED LIGHTS OUT

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 would change to the image on the right.

acm pku 1222 Extended lights out的算法分析_第1张图片

 acm pku 1222 Extended lights out的算法分析_第2张图片


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 illustrated in the second diagram, 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 should be exactly one space 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

Source

Greater New York 2002

 

分析: 这是一道进行模拟搜索的题目,但是并不是去搜索所有5×6=30个元素的“按”与“不按”的判断,如果真是这样,肯定会超时。事实上,完全可以根据第1行的选择(选择“按下”或“不按”第一行的六个按钮),就可以决定接下来的第2行按钮的状态,而根据第2行的状态又可以决定第3行的状态,……,以此类推,可以通过第1行的状态依次决定出第2,3,4,5行的状态,之后再判断第5行是否全部熄灭,若是,则成功,否则,失败。

注意三点:

l         搜索第1行的可能状态共有2^6=64种,时间足够;

l         i行某列的灯是否应该“按下”,由该行此列上一列的灯的状态所决定,而此时的状态有5个要素决定,即该灯的初始状态、该灯的上一列、前一行、自己一行、后一行的操作;

l         5行的灯的状态,有该灯的初始状态和该灯的上一列、前一行、自己一行、后一行的操作决定。

 

 

 

 

代码实现:

#include <string.h>

#include "iostream"

using namespace std;

 

//1WA

short Light[7][8];

short Press[7][8];

 

bool AllLightOff(void)

{

       int i, j;

       int temp = 64;

       bool flag;

 

       while(temp > -1)

       {

              temp-- ;

              flag = true;

              for(i = 0; i < 7; i++)

              {           

                     memset(Press[i], 0, sizeof(short)*8);

              }

              for(j = 1; j < 7; j++)

              {

                     Press[1][j] = ((temp>>(j-1))&1);

              }

 

              for(i = 2; i < 6; i++)

              {

                     for(j = 1; j < 7; j++)

                     {

                            Press[i][j] = (Light[i-1][j] + Press[i-2][j] + Press[i-1][j-1] + Press[i-1][j] + Press[i-1][j+1])%2;

                     }

              }

 

              for(j = 1; j < 7; j++)

              {

                                         {

                     if((Press[5][j-1] + Press[5][j] + Press[5][j+1] + Press[4][j]+ Light[5][j])%2 != 0)

 

                            flag = false;

                            break;

                     }

              }

              if(flag)

                     return true;

       }

}

 

int main(void)

{

       int n, tempn=0;

       int i, j;

 

       cin >> n;

       while(n > 0)

       {

              for(i = 1; i < 6; i++)

              {

                     for(j = 1; j < 7; j++)

                            cin >> Light[i][j];

              }

 

              while(!AllLightOff());

              cout << "PUZZLE #" << ++tempn << endl; //PUZZLE写成了PUZZlE WA1次,我faint

              for(i = 1; i < 6; i++)

              {

                     for(j = 1; j < 7; j++)

                     {

                            cout << Press[i][j] << " ";

                     }

                     cout << endl;

              }

              n-- ;

       }

 

       return 0;

}

 

提交结果:

Problem: 1222

 

User: uestcshe

Memory: 704K

 

Time: 0MS

Language: G++

 

Result: Accepted

 

你可能感兴趣的:(算法,input,each,button,iostream,output)