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.

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
20 1 1 0 1 01 0 0 1 1 10 0 1 0 0 11 0 0 1 0 10 1 1 1 0 00 0 1 0 1 01 0 1 0 1 10 0 1 0 1 11 0 1 1 0 00 1 0 1 0 0Sample Output
PUZZLE #11 0 1 0 0 11 1 0 1 0 10 0 1 0 1 11 0 0 1 0 00 1 0 0 0 0PUZZLE #21 0 0 1 1 11 1 0 0 0 00 0 0 1 0 01 1 0 1 0 11 0 1 1 0 1Source
Greater New York 2002
題意:有一個5*6的矩陣,每個位置都表示按鈕和燈,1表示亮,0表示滅。每當(dāng)按下一個位置的按鈕,它和它周圍燈的狀態(tài)全部翻轉(zhuǎn),問在這樣的一個方陣中按下哪些按鈕可以把整個方陣都變成滅的,這時1表示按了,0表示沒按。
思路: 轉(zhuǎn)自:http://blog.csdn.net/shiren_Bod/article/details/5766907
這個游戲有一些技巧: 1、按按鈕的順序可以隨便。 2、任何一個按鈕都最多需要按下1次。因為按下第二次剛好抵消第一次,等于沒有按。
這個問題可以轉(zhuǎn)化成數(shù)學(xué)問題。 一個燈的布局可以看成一個0、1矩陣。以3x3為例: 0 1 0 1 1 0 0 1 1 表示一個布局。其中0表示燈滅,1表示燈亮。 每次按下按鈕(POJ1222)或者叫一個宿舍關(guān)燈(0998),可以看成在原矩陣上加(模2加,就是按位異或)上一個如下的矩陣: 0 1 0 1 1 1 0 1 0 上述矩陣中的1表示按下第2行第2列的按鈕時,作用的范圍。如果按左上角的按鈕,就是: 1 1 0 1 0 0 0 0 0
我們記L為待求解的原始布局矩陣。A(i,j)表示按下第i行第j列的按鈕時的作用范圍矩陣。在上述例子中, L= 0 1 0 1 1 0 0 1 1
A(1,1)= 1 1 0 1 0 0 0 0 0
A(2,2)= 0 1 0 1 1 1 0 1 0
假設(shè)x(i,j)表示:想要使得L回到全滅狀態(tài),第i行第j列的按鈕是否需要按下。0表示不按,1表示按下。那么,這個游戲就轉(zhuǎn)化為如下方程的求解: L + x(1,1)*A(1,1) + x(1,2)*A(1,2) + x(1,3)*A(1,3) + x(2,1)*A(2,1) + ... + x(3,3)*A(3,3) = 0
其中x(i,j)是未知數(shù)。方程右邊的0表示零矩陣,表示全滅的狀態(tài)。直觀的理解就是:原來的L狀態(tài),經(jīng)過了若干個A(i,j)的變換,最終變成0:全滅狀態(tài)。 由于是0、1矩陣,上述方程也可以寫成: x(1,1)*A(1,1) + x(1,2)*A(1,2) + x(1,3)*A(1,3) + x(2,1)*A(2,1) + ... + x(3,3)*A(3,3) = L
這是一個矩陣方程。兩個矩陣相等,充要條件是矩陣中每個元素都相等。將上述方程展開,便轉(zhuǎn)化成了一個9元1次方程組: 簡單地記做:AA * XX = LL
這個方程有唯一解: x(1,1) x(1,2) x(1,3) x(2,1) x(2,2) x(2,3) x(3,1) x(3,2) x(3,3) = 1 1 1 0 0 0 0 0 1
也就是說,按下第一行的3個按鈕,和右下角的按鈕,就
| 能使L狀態(tài)變成全滅狀態(tài)。 對于固定行列的陣列來說,AA矩陣也是確定的。是否存在解,解是否唯一,只與AA矩陣有關(guān)。對于唯一解的情形,只要將LL乘以AA的逆矩陣即可。具體求AA的逆矩陣的方法,可以用高斯消元法。 由于是0、1矩陣,上述方程也可以寫成: 將1式兩邊同時加上一個L矩陣就可以變成x(1,1)*A(1,1) + x(1,2)*A(1,2) + x(1,3)*A(1,3) + x(2,1)*A(2,1) + ... + x(3,3)*A(3,3) = L A(1,1)把矩陣 轉(zhuǎn)化為一個列向量,L也轉(zhuǎn)化為一個列向量, 將sigma xi*Ai=Li 對應(yīng)位置的值相等就可以建立方程組了 X1*A(1,1)1+X2*A(1,2)1+X3*A(1,3)1+…………X30*A(30,30)1=L1; mod 2 X1*A(1,1)2+X2*A(1,2)2+X3*A(1,3)2+…………X30*A(30,30)2=L2; mod 2 X1*A(1,1)3+X2*A(1,2)3+X3*A(1,3)3+…………X30*A(30,30)3=L3 mod 2 ……. ……. ……. X1*A(1,1)30+X2*A(1,2)30+X3*A(1,3)30+…………X30*A(30,30)30=L30; mod 2 其中A(i,j)k 表示列向量A中第K個元素 這里的*表示點乘,Xi取(1,0) +表示模2加法,所以在高斯消元的時候可以用^異或運算 |
1個開關(guān)最多控制5個燈,在構(gòu)造的矩陣中,a[i][j]=1表示第i個開關(guān)可以影響到j(luò)號燈
代碼:
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int a[30][35];int equ=30,var=30;int free_x[30],x[30];void init(){ memset(a,0,sizeof(a)); for(int i=0;i<5;i++) for(int j=0;j<6;j++) { int t=i*6+j; a[t][t]=1; if(i>0) a[(i-1)*6+j][t]=1; if(i<4) a[(i+1)*6+j][t]=1; if(j>0) a[i*6+j-1][t]=1; if(j<5) a[i*6+j+1][t]=1; }}int gcd(int a, int b){ int t; while (b != 0) { t = b; b = a % b; a = t; } return a;}inline int lcm(int a, int b){ return a * b / gcd(a, b);}int Gauss(){ int i,j,k; int max_r; int col; int ta,tb; int LCM; int temp; int free_x_num; int free_index; for(k=0,col=0;k<30,col<30;k++,col++) { max_r=k; for(i=k+1;i<equ;i++) if(abs(a[i][col])>abs(a[max_r][col])) max_r=i; if(max_r!=k) for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]); if(a[k][col]==0) { k--;continue; } for(i=k+1;i<equ;i++) { if(a[i][col]!=0) { /*LCM=lcm(abs(a[i][col]),abs(a[k][col])); ta=LCM/abs(a[i][col]),tb=LCM/abs(a[k][col]); if(a[i][col]*a[k][col]<0) tb=-tb; for(j=col;j<var+1;j++) a[i][j]=a[i][j]*ta-a[k][j]*tb; */ for(j=col;j<var+1;j++) a[i][j]^=a[k][j]; } } } /*for(i=k;i<equ;i++) if(a[i][col]!=0) return -1; printf("%d %d/n",k,var); if(k<var) { for(i=k-1;i>=0;i--) { free_x_num=0; for(j=0;j<var;j++) if(a[i][j]!=0&&!free_x[j]) { free_x_num++; free_index=j; } if(free_x_num>1) continue; temp=a[i][var]; for(j=0;j<var;j++) if(a[i][j]!=0&&j!=free_index) { temp-=x[j]*a[i][j]; } x[free_index]=temp/a[i][free_index]; free_x[free_index]=1; } return var-k; } */ for(i=var-1;i>=0;i--) { /*temp=a[i][var]; for(j=i+1;j<var;j++) if(a[i][j]!=0) temp-=a[i][j]*x[j];*/ x[i]=a[i][30]; for(j=i+1;j<var;j++) x[i]^=(a[i][j]&&x[j]); } return 0;}int main(){ int T; scanf("%d",&T); for(int t=1;t<=T;t++) { memset(free_x,0,sizeof(free_x)); memset(x,0,sizeof(x)); init(); for(int i=0;i<30;i++) scanf("%d",&a[i][30]); int free_num=Gauss(); printf("PUZZLE #%d/n",t); for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { if(j==5){ printf("%d",x[i*6+j]);break;} printf("%d ",x[i*6+j]); } printf("/n"); } }}
新聞熱點
疑難解答