Poj1222 extended lights out, poj1222
Exclusive or Gaussian element:
The opening and closing of each lamp affect the surrounding five grids. Select some lights to turn off all the lights.
You can build a 30 × 30 matrix of the impact of each lamp on the surrounding area. The matrix value is equal to the original state.
Then, use Gauss to decode the status of each lamp.
EXTENDED LIGHTS OUT
Time Limit:1000 MS |
|
Memory Limit:10000 K |
Total Submissions:6442 |
|
Accepted:4228 |
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 wocould 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 specified strated in the second digoal, 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 shoshould 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 0 Sample 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 1 Source Greater New York 2002 |
[Submit] [Go Back] [Status] [Discuss]
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[40][40],ans[40];void Gauss(){ for(int k=0;k<30;k++) { int mx=k; for(int i=k+1;i<30;i++) if(a[i][k]>a[mx][k]) mx=i; if(mx) { for(int i=0;i<=30;i++) swap(a[mx][i],a[k][i]); } for(int i=0;i<30;i++) { if(i==k) continue; if(a[i][k]) { for(int j=k;j<=30;j++) { a[i][j]^=a[k][j]; } } } } for(int i=0;i<30;i++) ans[i]=a[i][30];}int main(){ int T_T,cas=1; scanf("%d",&T_T); while(T_T--) { memset(a,0,sizeof(a)); memset(ans,0,sizeof(ans)); for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { scanf("%d",&a[j+i*6][30]); a[j+i*6][j+i*6]=1; if(i-1>=0) a[j+i*6][j+6*(i-1)]=1; if(i+1<5) a[j+i*6][j+6*(i+1)]=1; if(j-1>=0) a[j+i*6][j-1+i*6]=1; if(j+1<6) a[j+i*6][j+1+i*6]=1; } } Gauss(); printf("PUZZLE #%d\n",cas++); for(int i=0;i<30;i++) { if(i%6) putchar(32); printf("%d",ans[i]); if((i+1)%6==0) putchar(10); } } return 0;}
Poj1222
Sunqian1998@sina.com
Send me an email and I will send it back and forth (by signature)
I am very grateful for the code of several programs of Peking University acm.
There are only 1166, hoping to help you. In fact, you can search Baidu for answers to poj questions on many blogs.
This is edited by myself:
# Include <iostream>
Using namespace std;
Int main ()
{
Int a [9] = {};
Int B [9] = {};
Int c [9] = {};
For (int I = 0; I <9; I ++)
Cin> a [I];
For (B [0] = 0; B [0] <4; B [0] ++)
{
For (B [1] = 0; B [1] <4; B [1] ++)
{
For (B [2] = 0; B [2] <4; B [2] ++)
{
For (B [3] = 0; B [3] <4; B [3] ++)
{
For (B [4] = 0; B [4] <4; B [4] ++)
{
For (B [5] = 0; B [5] <4; B [5] ++)
{
For (B [6] = 0; B [6] <4; B [6] ++)
{
For (B [7] = 0; B [7] <4; B [7] ++)
{
For (B [8] = 0; B [8] <4; B [8] ++)
{
If (B [0] + B [1] + B [3] + a [0]) % 4 = 0 & (B [0] + B [1] + B [2] + B [4] + a [1]) % 4 = 0 & (B [1] + B [2] + B [5] + a [2]) % 4 = 0 & (B [0] + B [3] + B [4] + B [6] + a [3]) % 4 = 0 & (B [0] + B [2] + B [4] + B [6] + B [8] + a [4]) % 4 = 0 & (B [2] + B [4] + B [5] + B [8] + a [5]) % 4 = 0 & (B [3] + B [6] + B [7] + a [6]) % 4 = 0 & (B [4] + B [6] + B [7] + B [8] + a [7]) % 4 = 0 & (B [5] + B [7] + B [8] + a [8]) % 4 = 0)
{
C [8] ++;
Break;
}
}
If (c [8]> 0)
Break ;}
If (c [8]> 0)
Break ;}
If (c [8]> 0)
Break ;}
If (c [8]> 0)
Break ;}
If (c [8]> 0)
Break ;}
If (c [8]> 0)
Break ;}
If (c [8]> 0)
Break ;}
If (c [8]> 0)
Break ;}
For (int j = 0; j <9; j ++)
{
While (B [j]! = 0)
{
Cout <j + 1 <"";
B [j] --;
}
}
Return 0;
}... Remaining full text>