POJ EXTENDED LIGHTS OUT 1222【高斯消元】


Language:
Default

EXTENDED LIGHTS OUT

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7672   Accepted: 4996

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

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

Source

Greater New York 2002

高斯消元经典问题

题意:给你一个5*6矩阵的灯,0代表关,1代表开。如果去开一个灯,那么它上下左右的灯的状态就改变。问怎么按这些灯,会关闭所有的灯。

分析:

第一道高斯消元!!!可以将状态考虑成矩阵相乘的形式, 因为是30个灯,所以每个灯的状态,进行所有的状态组合就是一个30*30的矩阵,而每个灯的状态设为一个未知量就有30个未知量,再构造30个方程。就转换为了高斯消元问题。

给定的末状态是b矩阵。初始状态为A矩阵,我们所要求的是就是A的线性组合等于b的情况。如何求这个矩阵?

因为给定的末状态是0矩阵。我们把0矩阵考虑成b矩阵,给定的初状态考虑成A矩阵。

然后我们所要求的就是求解x。

假定A是3*3矩阵:

0 1 0

A=
1 1 0

0 1 1

我们需要找出按下每个灯时的作用范围

1 1 0

a(1,1)= 1 0 0

0 0 0

0 1 0

a(2,2)=  1 1 1

0 1 0

我们找出所有灯按下之的影响范围,x代表这个灯是否按下。

我们考虑到A + 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。

因为是进行的异或运算。我们两边可以同时加上A矩阵。A^A就是0矩阵。所以等式变为:

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) =
A。

然后就是求增广矩阵。a|A。然后就是可以利用高斯消元求出x。

#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN=40;
int equ,var; //equ个方程,var个变元
int x[MAXN]; //解集
int a[MAXN][MAXN];//增广矩阵

int Gauss()
{
    int max_r;
    int col=0;
    //int i,j,k;
    for(int k=0;col<=var&&k<equ;k++,col++){
        max_r=k;
        for(int i=k+1;i<equ;i++) //查找最大变元
            if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
        if(max_r!=k) //交换最大变元所在行与当前行
            for(int i=col;i<=var;i++)
                swap(a[k][i],a[max_r][i]);
        if(!a[k][col]){ //当前变元为0,返回上一行
            k--;continue;
        }
        for(int i=k+1;i<equ;i++)
            if(a[i][col])//当前变元不为0
            for(int j=col;j<=var;j++)
                a[i][j]^=a[k][j];
    }
    for(int i=var-1;i>=0;i--){
        x[i]=a[i][var];
        for(int j=i+1;j<var;j++)
            x[i]^=(a[i][j]&&x[j]);
    }
    return 0;
}

void init() //初始化A矩阵。为灯的作用范围
{
    memset(a,0,sizeof(a));
    var=30;
    equ=30;
    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[t][i*6+j-1]=1;
            if(j<5) a[t][i*6+j+1]=1;
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    int xp=1;
    while(T--){
        init();
        for(int i=0;i<30;i++)
            scanf("%d",&a[i][30]);
        Gauss();
        printf("PUZZLE #%d\n",xp++);
        for(int i=0;i<30;i++){
            if((i+1)%6==0){
                printf("%d\n",x[i]);
                continue;
            }
            printf("%d ",x[i]);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-10-06 02:29:29

POJ EXTENDED LIGHTS OUT 1222【高斯消元】的相关文章

uva 1560 - Extended Lights Out(枚举 | 高斯消元)

题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5?6的矩阵,每个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,如果按了这个位置的开关,将会导致周围包括自己位置的灯状态变换,求一个按开关位置,保证所有灯都灭掉. 解题思路: 枚举,枚举第一行的状态,然后递推出后面四行的状态. 高斯消元,对于每个位置对定变量,这样列出30个方程求解. C++ 枚举 #include <cstdio> #include <cstring> #include &

POJ 1222 EXTENDED LIGHTS OUT(高斯消元解XOR方程组)

http://poj.org/problem?id=1222 题意:现在有5*6的开关,1表示亮,0表示灭,按下一个开关后,它上下左右的灯泡会改变亮灭状态,要怎么按使得灯泡全部处于灭状态,输出方案,1表示按,0表示不按. 思路:每个开关最多只按一次,因为按了2次之后,就会抵消了. 可以从结果出发,也就是全灭状态怎么按能变成初始状态. 用3*3来举个例子,$X\left ( i,j \right )$表示这些开关是按还是不按,那么对于第一个开关,对它有影响的就只有2.4这两个开关,所以它的异或方程

POJ 1222 EXTENDED LIGHTS OUT(高斯消元)

[题目链接] http://poj.org/problem?id=1222 [题目大意] 给出一个6*5的矩阵,由0和1构成,要求将其全部变成0,每个格子和周围的四个格子联动,就是说,如果一个格子变了数字,周围四格都会发生变化,变化即做一次与1的异或运算,输出每个格子的操作次数. [题解] 高斯消元练手题,对于每个格子的最终情况列一个方程,一共三十个方程三十个未知数,用高斯消元求解即可. [代码] #include <cstdio> #include <algorithm> #in

「ZOJ 1354」Extended Lights Out「高斯消元」

题意:给定一个\(5\times 6\)的棋盘的\(01\)状态,每次操作可以使它自己和周围四个格子状态取反,求如何操作,输出一个\(01\)矩阵 题解:这题可以通过枚举第一行的状态然后剩下递推来做,但是这里还是写一种好理解的高斯消元解异或方程组的方法. 对于每个格子列一个方程,未知数就是要求的答案矩阵,系数的话把它周围的设为1,其他设为0.然后右边的常数项为它本来的状态.然后就高斯消元嘛. 我用了bitset优化,实际上可能unsigned int或者long long也可以. #includ

【POJ】1222 EXTENDED LIGHTS OUT(高斯消元)

http://poj.org/problem?id=1222 竟然我理解了两天..... 首先先来了解异或方程组(或者说mod2方程组,modk的话貌似可以这样拓展出来) 对于一些我们需要求出的变量a[1~n],我们现在知道n个方程组(有解的情况下),每个方程均是类似原版消元那样带了个系数的,只不过这个系数只有0和1,那么我们第i个方程用x[i, 1~n]表示a[1~n]的系数,然后x[n+1]为这个方程的右式 那么这些方程组是这样的 (x[1,1]*a[1])^(x[1,2]*a[2])^..

POJ 1681---Painter&#39;s Problem(高斯消元)

POJ   1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a painter and he wants to paint all the bricks yellow. But there is something

POJ 1681 Painter&#39;s Problem (高斯消元)

题目地址:POJ 1681 跟前两题几乎一模一样的...不多说了.高斯消元+自由元枚举. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include &

POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)

题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现在还只是会套模板,不能独立的思考,好伤心.... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath&g

POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)

题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是还保留着一些对工人制造工具的记录,对于每个老员工,他的记录包括,他开始工作的时间(在某个星期的星期几),被炒鱿鱼的时间(某个星期的星期几),在第几个星期不知道.....在这段时间里,他正好加工了k件物品,给出了这k件物品的编号.我们要做的就是通过这些记录,来确定每种工具的加工时间是多少. 分析: 对