描述
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.
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:
- It does not matter what order the buttons are pressed.
- 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.
- 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.
输入
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's or 1's separated by one or more spaces. A 0 indicates that the light is off, while a 1 indicates that the light is on initially.
输出
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. 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's indicate buttons, which are not pressed. There should be exactly one space between each 0 or 1 in the output puzzle-like display.
题目给出一个 5 行 6 列的矩阵,0 表示熄灭,1 表示点亮,求出让所有灯都熄灭的一组开关的集合。
看北大的一个算法视频学到的。如果枚举全部的开关状态,时间复杂度将达到 O(2^30)。
这时可以考虑,是否只需枚举局部的解,使其他部分的解可以直接推出来或者其他部分需要枚举的量很小。
想一下,如果第 i 行的灯的状态确定了,那么我们通过改变第 i+1 行开关的状态,就可以使第 i 行的灯全灭,并且这时第 i+1 行的开关状态是被唯一确定的。
那么只需枚举第一行的开关状态,后面四行的开关状态就可以确定了,这时判断一下最后一行的灯是否全灭,如果全灭就已经找到了一个解了。
时间复杂度是 O(2^6 * 5 * 6)
代码:
#include <iostream> #include <cstring> using namespace std; const int MAX = 10; int puzzle[MAX][MAX], press[MAX][MAX]; //灯的状态和开关的状态 bool isOk(); int main(){ // freopen("input.txt", "r", stdin); int T; scanf("%d", &T); for(int k=1; k<=T; k++){ for(int i=1; i<=5; i++){ for(int j=1; j<=6; j++){ scanf("%d", &puzzle[i][j]); } } memset(press, 0, sizeof(press)); //枚举第一行的开关状态 int t = 0; while(t < (1<<6)){ for(int i=0; i<6; i++){ press[1][i+1] = ((t & (1<<i)) != 0); } //判断是否可行 if(isOk()){ printf("PUZZLE #%d ", k); for(int i=1; i<=5; i++){ for(int j=1; j<=6; j++){ printf("%d ", press[i][j]); } printf(" "); } break; } t++; } } return 0; } bool isOk(){ for(int i=2; i<=5; i++){ for(int j=1; j<=6; j++){ press[i][j] = (puzzle[i-1][j] + press[i-2][j] + press[i-1][j-1] + press[i-1][j] + press[i-1][j+1]) % 2; } } //判断最后一行的灯是否全灭 for(int i=1; i<=6; i++){ if((puzzle[5][i] + press[4][i] + press[5][i-1] + press[5][i] + press[5][i+1]) % 2 == 1) return false; } return true; }