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.
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.
翻译:给你一个5*6的初始状态,要求你给出一个5*6的操作矩阵,要求:当操作是1时,代表把棋盘的对应位置和它相邻的地方的状态改变(1变为0,0变为1),0则不进行操作。要求操作矩阵满足操作后棋盘状态全部为0.保证有解且唯一
提示:按两下和不按是一样的,所以只有按不按,没有按几下的区别。也没有先按后按的区别。
就是解异或方程。。
每个点按不按^周围的点按不按^最开始情况=0
转换一下。。
周围的点^每个点按不按=最开始情况
枚举每个点,之后就是喜闻乐见的高斯消元时间了。。(ps.如果消i元素,但是你的f[i][i]=0的话,需要找一行不等于0的行swap一下)
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 6 using namespace std; 7 8 int f[6][7],ans[6][7],ga[101][101],cnt=0; 9 10 void print() 11 { 12 cnt++; 13 printf("PUZZLE #%d\n",cnt); 14 for(int i=1;i<=5;i++) 15 { 16 for(int j=1;j<=6;j++) 17 printf("%d ",ans[i][j]); 18 printf("\n"); 19 } 20 } 21 22 void Solve() 23 { 24 for(int i=30;i>=1;i--) 25 { 26 int x=i/6+1,y=i%6; 27 if(!y) y+=6,x--; 28 ans[x][y]=ga[i][31]; 29 for(int j=i+1;j<=30;j++) 30 if(ga[i][j]){ 31 int x1=j/6+1,y1=j%6; 32 if(!y1) y1+=6,x1--; 33 ans[x][y]=ans[x][y]^ans[x1][y1]; 34 } 35 } 36 } 37 38 void swapp(int l,int r) 39 { 40 for(int i=1;i<=31;i++) 41 swap(ga[l][i],ga[r][i]); 42 } 43 44 void find(int n) 45 { 46 for(int i=n+1;i<=30;i++) 47 if(ga[i][n]){swapp(i,n);return;} 48 } 49 50 void Guass() 51 { 52 for(int i=2;i<=30;i++)//消第几个元 53 { 54 if(!ga[i-1][i-1])find(i-1); 55 if(!ga[i-1][i-1])continue; 56 for(int j=i;j<=30;j++)//第几个方程 57 { 58 if(!ga[j][i-1])continue; 59 for(int k=i;k<=31;k++)//方程的第几项 60 ga[j][k]=ga[j][k]^ga[i-1][k]; 61 } 62 } 63 Solve(); 64 } 65 66 void set() 67 { 68 for(int i=1;i<=5;i++) 69 for(int j=1;j<=6;j++){ 70 ga[(i-1)*6+j][31]=f[i][j]; 71 ga[(i-1)*6+j][(i-1)*6+j]=1; //自己和上下左右是对自己有影响的点 72 if(j!=1) ga[(i-1)*6+j][(i-1)*6+j-1]=1; 73 if(j!=6) ga[(i-1)*6+j][(i-1)*6+j+1]=1; 74 if(i!=5) ga[(i-1)*6+j][i*6+j]=1; 75 if(i!=1) ga[(i-1)*6+j][(i-2)*6+j]=1; 76 } 77 return; 78 } 79 80 int main() 81 { 82 int T; 83 scanf("%d",&T); 84 while(T--) 85 { 86 for(int i=1;i<=5;i++) 87 for(int j=1;j<=6;j++) 88 scanf("%d",&f[i][j]); 89 set(); 90 Guass(); 91 print(); 92 memset(f,0,sizeof(f)); 93 memset(ga,0,sizeof(ga)); 94 memset(ans,0,sizeof(ans)); 95 } 96 return 0; 97 }