EXTENDED LIGHTS OUT
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5449 | Accepted: 3601 |
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.
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.
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
题意:给出一个5*6的矩阵,矩阵中元素的值为1或0,改变一个元素的值(0变为1,1变为0),元素的四个相邻位置(上下左右)的值都会被改变。现在要求出改变哪些元素的值,可以使得矩阵中全部元素变为0.
思路:dfs。我们只需枚举第一行每个元素的值(变或不变),如果第一行元素(1,j)为1,那么必然要改变第二行元素(2,j)的值。如果(1,j)为0,则不需要改变。第三行也是这样,根据第二行来改变,以此类推....。当枚举完第一行元素的值时,因为我们已经使得前面的元素全为0,这时看第五行元素是否全为0即可。若全为0,输出答案,否则还原本来的矩阵,继续dfs。
AC代码:
#include <cstring> #include <string> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <vector> #include <cstdlib> #include <iostream> using namespace std; int d[4][2]={{-1,0},{0,1},{0,-1},{1,0}}; int map[10][10]; int ans[10][10]; //记录答案 bool off; //成功令矩阵元素全为0的标志 bool ok(int x,int y) //判断元素的位置是否合法 { if(x>=1&&x<=5&&y>=1&&y<=6) return true; return false; } void press(int x,int y) //改变某元素的值 { map[x][y]^=1; int nx,ny; for(int i=0;i<4;i++) { nx=x+d[i][0],ny=y+d[i][1]; if(ok(nx,ny)) map[nx][ny]^=1; } } void dfs(int cur) { if(off) return; if(cur==7) { for(int i=2;i<=5;i++) for(int j=1;j<=6;j++) if(map[i-1][j]) { press(i,j); ans[i][j]=1; } bool flag=true; for(int i=1;i<=6;i++) //判断第5行是否全为0 if(map[5][i]) { flag=false; break; } if(!flag) //失败,还原本来的矩阵 { for(int i=2;i<=5;i++) for(int j=1;j<=6;j++) if(ans[i][j]==1) { press(i,j); ans[i][j]=0; } return; } else //成功,输出 { for(int j=1;j<=5;j++) { for(int k=1;k<=6;k++) printf("%d ",ans[j][k]); printf("\n"); } off=true; return; } } press(1,cur); //尝试改变该值 ans[1][cur]=1; dfs(cur+1); press(1,cur); //尝试不改变该值 ans[1][cur]=0; dfs(cur+1); } int main() { int t; int c=0; scanf("%d",&t); while(t--) { c++; memset(ans,0,sizeof(ans)); for(int i=1;i<=5;i++) for(int j=1;j<=6;j++) scanf("%d",&map[i][j]); printf("PUZZLE #%d\n",c); off=false; dfs(1); } return 0; }
还可用高斯消元法解:
AC代码:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <vector> #include <algorithm> #define ll __int64 #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) using namespace std; const int INF = 2000000000; const int maxn = 35; int ans[maxn]; int a[maxn][maxn]; int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; bool ok(int x, int y){ if(x >= 0 && x < 5 && y >= 0 && y < 6) return true; return false; } void gauss(){ for(int i = 0; i < 30; i++) { int k = i; for(; k < 30; k++) if(a[k][i]) break; for(int j = 0; j <= 30; j++) swap(a[i][j], a[k][j]); for(int j = 0; j < 30; j++) if(j != i && a[j][i]) { for(k = 0; k <= 30; k++) a[j][k] ^= a[i][k]; } } } int main() { int t; scanf("%d", &t); for(int c = 1; c <= t; c++) { memset(a, 0, sizeof(a)); for(int i = 0; i < 30; i++) { scanf("%d", &a[i][30]); ans[i] = 0; } for(int i = 0; i < 5; i++) for(int j = 0; j < 6; j++) { int row = i * 6 + j; a[row][row] = 1; for(int k = 0; k < 4; k++) { int nx = i + d[k][0], ny = j + d[k][1]; if(ok(nx, ny)) a[row][nx * 6 + ny] = 1; } } gauss(); for(int i = 0; i < 30; i++) ans[i] = a[i][30]; printf("PUZZLE #%d\n", c); for(int i = 0; i < 30; i++) { printf("%d", ans[i]); if(i % 6 == 5) puts(""); else printf(" "); } } return 0; }