POJ 1222 EXTENDED LIGHTS OUT(高斯消元)

来源:互联网 发布:java简历 编辑:程序博客网 时间:2024/05/20 23:03

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

题目大意

已知5*6的方格,其中每个格子都有一个按钮控制着灯的开关状态,当一个灯的开关状态改变时,上下左右四个方向的灯的开关状态也会发生反转。问按下哪些灯的按钮,会使全部的灯都熄灭。输出一个相对应的矩阵,其中1代表按下按钮,0代表不按按钮。

解题思路

利用高斯消元解异或方程组。
高斯消元求解的变型,由矩阵的加减转化为异或运算(因为只是0、1矩阵,每两个对应元素相加%2)。
其中5*6=30个格子对应着30个方程,每个灯是否被按下即代表一个变量,每个灯的状态值与周围的四个灯的状态值有关。
已2*3矩阵为例说明:
假设起初灯的状态为

L=147258369

那么当按下某个灯的按钮时,其带来的变化可以通过异或一个矩阵来实现,比如(A[i,j]代表按下第 i 行、第 j 列的按钮):

A(1,1)=[111000]A(1,2)=[101110]A(1,3)=[001011]

A(2,1)=[110100]A(2,2)=[011101]A(2,3)=[000111]

另外存在一个矩阵X,X(i,j)代表要将方格中每个灯灭掉时每个格子中的按钮是否被按下,为0、1矩阵。由此可得到关系方程(相当于矩阵的点乘):
L xor [X(1,1)*A(1,1)] xor [X(1,2)*A(1,2)] xor [X(1,3)*A(1,3)] xor [X(2,1)*A(2,1)] xor [X(2,2)*A(2,2)] xor [X(2,3)*A(2,3)]=0

方程两边同时异或上L可得到:
[X(1,1)*A(1,1)] xor [X(1,2)*A(1,2)] xor [X(1,3)*A(1,3)] xor [X(2,1)*A(2,1)] xor [X(2,2)*A(2,2)] xor [X(2,3)*A(2,3)]=L

由左边矩阵和右边矩阵每个元素对应相等可得到6个格点所对应的方程组(使用ai,bi,ci,di,ei,fi分别代表A矩阵中的6个元素):

a1*X(1,1) xor b1*X(1,2) xor c1*X(1,3) xor d1*X(2,1) xor e1*X(2,2) xor f1*X(2,3)=L11,
a2*X(1,1) xor b2*X(1,2) xor c2*X(1,3) xor d2*X(2,1) xor e2*X(2,2) xor f2*X(2,3)=L12,
……
a6*X(1,1) xor b6*X(1,2) xor c6*X(1,3) xor d6*X(2,1) xor e6*X(2,2) xor f6*X(2,3)=L23;
使用高斯消元解方程组即可(加减符号在运算过程中变换为&,^符号)。
PS:在本题中,”认为”有唯一解,其实对于示例跑出来的只是一组特解。

代码实现

1、消元与回代过程合并

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;#define maxn 37#define length 6#define row 3int a[maxn][maxn];   //系数矩阵int ans[maxn];       //存放结果void Gauss(){    int i, j, k, r;    for(k=0; k<length; k++)    {        i = k;        while(i<length && a[i][k]==0)  i++;        if(i > k)        {            for(r=0; r<=length; r++)                swap(a[i][r], a[k][r]);        }        for(i=0; i<length; i++)        {              if(i != k && a[i][k])            {                for(j=0; j<=length; j++)                    a[i][j] ^= a[k][j];            }        }    }    for(i=0; i<length; i++)        if(a[i][length])        {            for(j=0; j<length && !a[i][j]; j++)  ;            if(j == length) return;            else                ans[j] = a[i][length];        }    return;}int main(){    int i,j,T;    scanf("%d",&T);    for(int t=1; t<=T; t++)    {        memset(a, 0, sizeof(a));        for(i=0; i<length; i++)        {            scanf("%d", &a[i][length]);            ans[i] = 0;        }        for(i=0; i<length; i++)        {            int n = i / row;            int m = i % row;            for(j=0; j<length; j++)            {                int x = j / row;                int y = j % row;                if(fabs(x - n) + fabs(y - m) <= 1)                    a[i][j] = 1;                else                    a[i][j] = 0;            }        }        Gauss();        printf("PUZZLE #%d\n",t);        for(i=0; i<length; i++)        {            printf("%d", ans[i]);            if((i+1) % row == 0)                printf("\n");            else                printf(" ");        }    }    return 0;}

2、先消元、后回代

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;#define maxn 37#define length 30#define row 6int a[maxn][maxn];int ans[maxn];void Gauss(){    int i, j, k, r;    for(k=0; k<length; k++)   //消元    {        i = k;        while(i<length && a[i][k]==0)  i++;        if(i > k)        {            for(r=0; r<=length; r++)                swap(a[i][r], a[k][r]);        }        for(i=k+1; i<length; i++)        {            if(a[i][k])            {                for(j=0; j<=length; j++)                    a[i][j] ^= a[k][j];            }        }    }    for(i=length-1;i>=0;i--)    //回代    {        ans[i]=a[i][length];        for(j=i+1;j<length;j++)            ans[i]^=(a[i][j]&&ans[j]);    }    return;}int main(){    int i,j,T;    scanf("%d",&T);    for(int t=1; t<=T; t++)    {        memset(a, 0, sizeof(a));        for(i=0; i<length; i++)        {            scanf("%d", &a[i][length]);            ans[i] = 0;        }        for(i=0; i<length; i++)        {            int n = i / row;            int m = i % row;            for(j=0; j<length; j++)            {                int x = j / row;                int y = j % row;                if(fabs(x - n) + fabs(y - m) <= 1)                    a[i][j] = 1;                else                    a[i][j] = 0;            }        }        Gauss();        printf("PUZZLE #%d\n",t);        for(i=0; i<length; i++)        {            printf("%d", ans[i]);            if((i+1) % row == 0)                printf("\n");            else                printf(" ");        }    }    return 0;}