POJ 1222 EXTENDED LIGHTS OUT(高斯消元异或矩阵)

来源:互联网 发布:淘宝店铺叫虎扑伙伴 编辑:程序博客网 时间:2024/05/22 07:51

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.

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

题意:问你关灯的方案使全部的灯都关上

分析:对于每个灯关上后会影响四个方向我们构造一个矩阵表示,n=3时

E(1)=x1*A(1,1) ^ x2*A(1,2) ^x3*A(1,3).

E(2)=x1*A(2,1) ^ x2*A(2,1) ^x3*A(2,3)

E(3)=x1*A(3,1) ^ x2*A(3,2) ^x3*A(3,3)

A(i,j)代表第j个开关会影响第i个开关.

其他的就是高消了。

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;int t,n;int mp[35][35];int ans[35];//void Gauss()//增广矩阵{    for(int i=0;i<30;i++)    {        int maxl=i;        for(int j=i;j<30;j++)            if(mp[j][i])            {                maxl=j;                break;            }        if(maxl!=i&&maxl<30)            for(int j=0;j<=30;j++)              swap(mp[maxl][j],mp[i][j]);        for(int j=i+1;j<30;j++)        {            if(mp[j][i])                for(int k=i;k<=30;k++)                   mp[j][k]^=mp[i][k];        }    }    for(int i=29;i>=0;i--)    {        ans[i]=mp[i][30];        for(int j=29;j>i;j--)            ans[i]^=(mp[i][j]&&ans[j]);//亦或变换    }}int main(){    int cas=1;    int nx,ny,sx,sy;    scanf("%d",&t);    while(t--)    {        for(int i=0;i<30;i++)            scanf("%d",&mp[i][30]);        for(int i=0;i<30;i++)        {            nx=i/6;ny=i%6;            for(int j=0;j<30;j++)            {                sx=j/6;sy=j%6;                if(abs(nx-sx)+abs(ny-sy)<=1)                    mp[i][j]=1;                else mp[i][j]=0;            }        }        Gauss();        printf("PUZZLE #%d\n",cas++);        for(int i=0;i<30;i++)        {            printf("%d",ans[i]);            if((i+1)%6==0)                puts("");            else printf(" ");        }    }    return 0;}//wocao


0 0
原创粉丝点击