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

field=source&key=Greater+New+York+2002" style="text-decoration:none">Greater New York 2002

给你一个5*6的矩阵。每一个矩阵单位里面都有一盏灯,你能够按下开关,使得此灯和周围的灯都改变状态,给你一个初始状态,让你求如何按才干使得全部的灯都灭。

能够定义30个未知数各自是x0,x1,x2,x3....x29代表每盏灯的开关是否被按。对于每一盏灯都能列一个方程式,对于在位置(i,j)的灯。能够列出:

x(i*6+j)+x(i*6+j+1)+x(i*6+j-1)+x((i+1)*6+j)+x((i-1)*6+j)=a%2  当中a为题中给出的初始开关的状态。

这样的方程能够列出30个,高斯消元就可以解出答案。

//140K	0MS
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<stack>
#include<queue>
#include<vector>

#pragma comment(linker, "/STACK:1024000000");
#define M 100007
#define inf 0x3f3f3f3f
#define ll long long
#define mod 1000000009
#define eps (1e-8)
using namespace std;
int matrix[37][37],id[37];
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
void Guess(int n,int m)
{
    int i=0,j=0;
    while(i<n&&j<m)
    {
        int max_i=i;
        for(int k=i+1;k<n;k++)
            if(matrix[k][j]==1)
            {
                max_i=k;
                break;
            }
        if(matrix[max_i][j])
        {
            if(max_i!=i)
                for(int k=0;k<m;k++)
                    swap(matrix[max_i][k],matrix[i][k]);
            for(int u=0;u<n;u++)
                if(i!=u&&matrix[u][j])
                    for(int k=j;k<m;k++)
                        matrix[u][k]=matrix[u][k]^matrix[i][k];
            i++;
        }
        j++;
    }
}
int main()
{
    int n,cas=1;
    scanf("%d",&n);
    while(n--)
    {
        memset(matrix,0,sizeof(matrix));
        memset(id,0,sizeof(id));
        for(int i=0;i<5;i++)
            for(int j=0;j<6;j++)
            {
                scanf("%d",&id[i*6+j]);
                matrix[i*6+j][30]=id[i*6+j];
            }
        for(int i=0;i<5;i++)
            for(int j=0;j<6;j++)
            {
                matrix[i*6+j][i*6+j]=1;
                for(int k=0;k<4;k++)
                {
                    int x=i+dir[k][0];
                    int y=j+dir[k][1];
                    if(x>=0&&x<5&&y>=0&&y<6)
                        matrix[i*6+j][x*6+y]=1;
                }
            }
        Guess(30,31);
        printf("PUZZLE #%d\n",cas++);
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<6;j++)
                printf("%d ",matrix[i*6+j][30]);
            printf("\n");
        }
    }
}

时间: 2024-10-11 18:23:20

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

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

UVA 1560 - Extended Lights Out(高斯消元)

UVA 1560 - Extended Lights Out 题目链接 题意:给定一个矩阵,1代表开着灯,0代表关灯,没按一个开关,周围4个位置都会变化,问一个按的方法使得所有灯都变暗 思路:两种做法: 1.枚举递推 这个比较简单,就枚举第一行,然后递推过去,每次如果上一行是亮灯,则下一行开关必须按下去 2.高斯消元, 这个做法比较屌一些,每个位置对应上下左右中5个位置可以列出一个异或表达式,然后30个位置对应30个异或表达式,利用高斯消元法就能求出每个位置的解了 代码: 高斯消元法: #inc

POJ 1222【异或高斯消元|二进制状态枚举】

题目链接:[http://poj.org/problem?id=1222] 题意:Light Out,给出一个5 * 6的0,1矩阵,0表示灯熄灭,反之为灯亮.输出一种方案,使得所有的等都被熄灭. 题解:首先可以用高斯消元来做,对于每个点,我们列出一个方程,左边是某个点和它相邻的点,他们的异或值等于右边的值(灯亮为1 ,灯灭为0),然后求一个异或高斯消元就可以了.可以用bitset优化,或者__int128优化(其实unsigned就可以了). 还可以枚举第一行的按开关的状态共有1<<6中状态

【POJ1222】EXTENDED LIGHTS OUT 高斯消元、解异或方程组

#include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/43481693"); } 题意: 多组数据. 有个5*6的图,然后你要对某些位置进行操作,使得最后灯的状态如图. 操作:这个灯位置的上下左右以及自己这五盏灯状态都取反. 然后输出操作. 说实话什么亮灭什么我全都没考虑. 直接瞎写一遍就PE了,

EXTENDED LIGHTS OUT (高斯消元)

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, be

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件物品的编号.我们要做的就是通过这些记录,来确定每种工具的加工时间是多少. 分析: 对