Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9310 | Accepted: 6048 |
Description
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
Output
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 11 0 1 1 0 1
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int s[10][10], flip[10][10], opt[10][10]; int dir[5][2]={{-1,0},{1,0},{0,0},{0,-1},{0,1}}; int cal(); int get(int x,int y); int main() { int n, ncase=1; scanf("%d", &n); while(n--) { for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { scanf("%d",&s[i][j]); } } int res=-1; for(int i=0;i<1<<6;i++) { memset(flip,0,sizeof(flip)); for(int j=0;j<6;j++) { flip[0][6-j-1]=(i>>j)&1; } int num=cal(); if((res==-1||res>num)&&num>=0) { res=num; memcpy(opt,flip,sizeof(flip)); } } printf("PUZZLE #%d\n",ncase++); for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { printf("%d%c",opt[i][j],j==5?'\n':' '); } } } return 0; } int cal() { for(int i=1;i<5;i++) { for(int j=0;j<6;j++) { if(get(i-1,j)%2!=0) { flip[i][j]=1; } } } for(int i=0;i<6;i++) { if(get(4,i)%2!=0) { return -1; } } int num=0; for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { num+=flip[i][j]; } } return num; } int get(int x,int y) { int num=s[x][y]; for(int i=0;i<5;i++) { int a=dir[i][0]+x, b=dir[i][1]+y; if(a>=0&&a<5&&b>=0&&b<6) { num+=flip[a][b]; } } return num; }