poj 1222 EXTENDED LIGHTS OUT 高斯消元法

EXTENDED LIGHTS OUT

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6872   Accepted: 4532

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

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int a[31][31];

void gauss_elimination(){
    int i,j,k;
    for(i=0;i<30;i++){
        k=i;
        for(;k<30;k++)
            if(a[k][i])  break;
        for(j=0;j<=30;j++)
            swap(a[i][j],a[k][j]);
        for(j=0;j<30;j++)
            if(j!=i&&a[j][i])
            for(k=0;k<=30;k++)
                a[j][k]^=a[i][k];
    }
}

int main(){
    int T,cas=0;
    cin>>T;
    while(T--){
        int i,j;
        memset(a,0,sizeof a);
        for(i=0;i<30;i++)
            cin>>a[i][30];

        for(i=0;i<30;i++){
            a[i][i]=1;
            if(i%6!=0) a[i-1][i]=1;
            if(i%6!=5) a[i+1][i]=1;
            if(i>5) a[i-6][i]=1;
            if(i<24) a[i+6][i]=1;
        }
        gauss_elimination();
        cout<<"PUZZLE #"<<++cas<<endl;
        for(i=0;i<30;i++){
            cout<<a[i][30];
            if(i%6==5)  cout<<endl;
            else  cout<<" ";
        }
    }
    return 0;
}
时间: 2024-10-14 00:02:35

poj 1222 EXTENDED LIGHTS OUT 高斯消元法的相关文章

POJ 1222 EXTENDED LIGHTS OUT 高斯消元

点击打开链接 EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6492   Accepted: 4267 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 butt

[POJ 1222] EXTENDED LIGHTS OUT

题目 http://acm.pku.edu.cn/JudgeOnline/problem?id=1222 描述 给你一个5行6列的矩阵分别表示30个灯,矩阵map[i][j]为1表示灯亮着, 0表示灯没亮. 要求你输出解决方案. press[i][j]为1表示按一下,0表示不按.使得最后状态为所有灯都熄灭. 分析 高斯消元法(Gaussian elimination method) => 对于每个格子 map[i][j], 能影响他的格子为 (i-1, j), (i, j-1), (i+1, j

POJ 1222 extended lights out 高斯消元 板子题

题目链接:http://poj.org/problem?id=1222 题目描述:其实就是开关问题, 按下按钮会影响当前和周围的四个按钮, 问关闭所有灯的方案 解题思路:以前用搜索做过, 那时候是刚刚接触ACM的时候, 当时劲头真足啊, 这个解释的很好:http://blog.csdn.net/u013508213/article/details/47263183 代码: #include <iostream> #include <cstdio> #include <cstr

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

【高斯消元】Poj 1222:EXTENDED LIGHTS OUT

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) neighbo

【高斯消元】 POJ 1222 EXTENDED LIGHTS OUT

通道 题意:有一个5*6的矩阵,每个位置都表示按钮和灯,1表示亮,0表示灭.每当按下一个位置的按钮,它和它周围灯的状态全部翻转,问在这样的一个方阵中按下哪些按钮可以把整个方阵都变成灭的,这时1表示按了,0表示没按 代码: #include <cstdio> #include <cstring> #include <vector> #include <cmath> #include <algorithm> using namespace std;

【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 1222】EXTENDED LIGHTS OUT

EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7333 Accepted: 4792 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).