程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj1222 EXTENDED LIGHTS OUT

poj1222 EXTENDED LIGHTS OUT

編輯:C++入門知識

poj1222 EXTENDED LIGHTS OUT


EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7202 Accepted: 4718

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 Z喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcmRlciB0aGUgYnV0dG9ucyBhcmUgcHJlc3NlZC4gPGJyPgoyLiBJZiBhIGJ1dHRvbiBpcyBwcmVzc2VkIGEgc2Vjb25kIHRpbWUsIGl0IGV4YWN0bHkgY2FuY2VscyB0aGUgZWZmZWN0IG9mIHRoZSBmaXJzdCBwcmVzcywgc28gbm8gYnV0dG9uIGV2ZXIgbmVlZCBiZSBwcmVzc2VkIG1vcmUgdGhhbiBvbmNlLiA8YnI+CjMuIEFzIGlsbHVzdHJhdGVkIGluIHRoZSBzZWNvbmQgZGlhZ3JhbSwgYWxsIHRoZSBsaWdodHMgaW4gdGhlIGZpcnN0IHJvdyBtYXkgYmUgdHVybmVkIG9mZiwgYnkgcHJlc3NpbmcgdGhlIGNvcnJlc3BvbmRpbmcgYnV0dG9ucyBpbiB0aGUgc2Vjb25kIHJvdy4gQnkgcmVwZWF0aW5nIHRoaXMgcHJvY2VzcyBpbiBlYWNoIHJvdywgYWxsIHRoZSBsaWdodHMgaW4gdGhlIGZpcnN0IDxicj4KZm91ciByb3dzIG1heSBiZSB0dXJuZWQgb3V0LiBTaW1pbGFybHksIGJ5IHByZXNzaW5nIGJ1dHRvbnMgaW4gY29sdW1ucyAyLCAzID8sIGFsbCBsaWdodHMgaW4gdGhlIGZpcnN0IDUgY29sdW1ucyBtYXkgYmUgdHVybmVkIG9mZi4gPGJyPgpXcml0ZSBhIHByb2dyYW0gdG8gc29sdmUgdGhlIHB1enpsZS4KPHAgY2xhc3M9"pst">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

分析來源:http://www.xuebuyuan.com/1394020.html

題意:有一個5*6的矩陣,每個位置都表示按鈕和燈,1表示亮,0表示滅。每當按下一個位置的按鈕,它和它周圍燈的狀態全部翻轉,問在這樣的一個方陣中按下哪些按鈕可以把整個方陣都變成滅的,這時1表示按了,0表示沒按。

以下分析部分轉自:http://blog.csdn.net/shiren_Bod/article/details/5766907

這個游戲有一些技巧:
1、按按鈕的順序可以隨便。
2、任何一個按鈕都最多需要按下1次。因為按下第二次剛好抵消第一次,等於沒有按。

這個問題可以轉化成數學問題。
一個燈的布局可以看成一個0、1矩陣。以3x3為例:
0 1 0
1 1 0
0 1 1
表示一個布局。其中0表示燈滅,1表示燈亮。
每次按下按鈕(POJ1222)或者叫一個宿捨關燈(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

假設x(i,j)表示:想要使得L回到全滅狀態,第i行第j列的按鈕是否需要按下。0表示不按,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) = 0

其中x(i,j)是未知數。方程右邊的0表示零矩陣,表示全滅的狀態。直觀的理解就是:原來的L狀態,經過了若干個A(i,j)的變換,最終變成0:全滅狀態。
由於是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

這是一個矩陣方程。兩個矩陣相等,充要條件是矩陣中每個元素都相等。將上述方程展開,便轉化成了一個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狀態變成全滅狀態。
對於固定行列的陣列來說,AA矩陣也是確定的。是否存在解,解是否唯一,只與AA矩陣有關。對於唯一解的情形,只要將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)把矩陣 轉化為一個列向量,L也轉化為一個列向量,

將sigma xi*Ai=Li 對應位置的值相等就可以建立方程組了

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個開關最多控制5個燈,在構造的矩陣中,a[i][j]=1表示第i個開關可以影響到j號燈

構造出的矩陣如圖:

\

參考代碼:

#include 
#include 
using namespace std;
int map[32][32];
int ans[32];
void Guass(){
	for (int i=0;i<30;i++){	//控制行
		if (map[i][i]==0){	
			for (int j=i+1;j<30;j++){	//找到不為0的那一行,然後進行交換
				if (map[j][i]!=0){
					for (int k=i;k<31;k++){
						swap(map[j][k],map[i][k]);
					}
					break;
				}
			}
		}
		
		for (int j=0;j<30;j++){
			if (i!=j&&map[j][i]){
				for (int k=i;k<31;k++){
					map[j][k]=map[i][k]^map[j][k];
				}
			}
		}
	}
	for (int i=0;i<30;i++){
		ans[i]=map[i][30];
	}
}
int main(){
	int t,kn,km,kx,ky;
	cin>>t;
	for (int cas=1;cas<=t;cas++){
		for (int i=0;i<30;i++)
			cin>>map[i][30];
		for(int i=0;i<30;i++){      //構造30個方程    
            kn=i/6;  
            km=i%6;  
            for(int j=0;j<30;j++){  
                kx=j/6;   
                ky=j%6;  
                if(abs(kx-kn)+abs(ky-km)<=1)  
                    map[i][j]=1;  
                else  
                    map[i][j]=0;  
            }  
        }
        Guass();
        cout<<"PUZZLE #"<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved