【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). 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

高斯消元解异或方程组。

首先把数组标号为id[i][j],后文所说的第k个就是指他的标号id[i][j]=k。

①我们用a[i][j]表示读入的方格;

②f[i][j]表示第i个方格对第j个方格有影响,即你对第i个方格执行开关操作时,j方格也被操作了(j在i的十字形区域内);

③最后的答案数组为ans[i],ans[i]=1表示第i个方格被执行操作了;

④tot=n?m。

对于一个方格(i,j),最后状态为0,设id[i][j]=k,我们可以列出这样的式子

a[i][j]+f[k][1]?ans[1]+f[k][2]?ans[2]+...f[k][tot]?ans[tot]=0(mod2)

因为对方程mod 2相当于取异或,我们可以把a[i][j]移到右边,方程变为:

f[k][1]?ans[1]+f[k][2]?ans[2]+...f[k][tot]?ans[tot]=a[i][j](mod2)

那么现在有tot个方程,tot个未知数,高斯消元就可以直接出来了~

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#define N 5
#define M 6
using namespace std;
int a[35][35],ans[35],id[10][10],d[15][3];
void Gauss(int n,int m)
{
    for (int i=1;i<=n;i++)
    {
        int j;
        for (j=i;j<=n&&!a[i][j];j++);
        if (j==n+1) continue;
        if (i!=j)
            for (int k=i;k<=m;k++)
                swap(a[i][k],a[j][k]);
        for (int j=i+1;j<=n;j++)
            if (a[j][i])
                for (int k=i;k<=m;k++)
                    a[j][k]^=a[i][k];
    }
    for (int i=n;i;i--)
    {
        for (int j=n;j>i;j--)
            a[i][m]^=(a[i][j]*a[j][m]);
        ans[i]=a[i][m];
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    int cnt=0;
    for (int i=1;i<=N;i++)
        for (int j=1;j<=M;j++)
            id[i][j]=++cnt;
    d[0][1]=d[0][2]=0;
    d[1][1]=d[2][1]=0,d[1][2]=1,d[2][2]=-1;
    d[3][2]=d[4][2]=0,d[3][1]=1,d[4][1]=-1;
    for (int t=1;t<=T;t++)
    {
        memset(a,0,sizeof(a));
        printf("PUZZLE #%d\n",t);
        for (int i=1;i<=N;i++)
            for (int j=1;j<=M;j++)
                for (int k=0;k<=4;k++)
                {
                    int p=id[i+d[k][1]][j+d[k][2]];
                    if (p) a[id[i][j]][p]=1;
                    else a[id[i][j]][p]=0;
                }
        for (int i=1;i<=N;i++)
            for (int j=1;j<=M;j++)
                scanf("%d",&a[id[i][j]][31]);
        Gauss(cnt,cnt+1);
        int now=0;
        for (int i=1;i<=N;i++)
        {
            printf("%d",ans[++now]);
            for (int j=2;j<=M;j++)
                printf(" %d",ans[++now]);
            printf("\n");
        }
    }
    return 0;
}

感悟:

WA是因为没有清零a数组,导致一些本来是0的在之后变成1了。

时间: 2024-10-11 01:27:10

【POJ 1222】EXTENDED LIGHTS OUT的相关文章

【POJ 1222】 EXTENDED LIGHTS OUT

[题目链接] http://poj.org/problem?id=1222 [算法] 列出异或方程组,用高斯消元求解即可 [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstd

POJ - 1222: EXTENDED LIGHTS OUT (开关问题-高斯消元)

pro:给定5*6的灯的状态,如果我们按下一个灯的开关,它和周围4个都会改变状态.求一种合法状态,使得终状态全为关闭: sol:模2意义下的高斯消元. 终于自己手打了一个初级板子. #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; int a[40][40],ans[40]; int x[5]={0,0,0,1,-1}; int y[5]={0,1,-1,0,0};

【POJ 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

【POJ 2513】Colored Sticks

[POJ 2513]Colored Sticks 并查集+字典树+欧拉通路 第一次做这么混的题..太混了-- 不过题不算难 字典树用来查字符串对应图中的点 每个单词做一个点(包括重复单词 题意就是每个边走且直走一次(欧拉通路 欧拉图的判定: 没有或者只有两个奇数度的点的图叫做欧拉图 有这些就可以解答此题了 另外需要注意题目范围是25W个木棍 所以最多可能有50W个点 卡了好多个RE 代码如下: #include <iostream> #include <cstdlib> #incl

2292: 【POJ Challenge 】永远挑战

2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 553  Solved: 230[Submit][Status][Discuss] Description lqp18_31和1tthinking经常出题来虐ftiasch.有一天, lqp18_31搞了一个有向图,每条边的长度都是1. 他想让ftiasch求出点1到点 N 的最短路."水题啊.", ftiasch这么说道. 所以1tth

【POJ 1201】 Intervals(差分约束系统)

[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23817   Accepted: 9023 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p

【POJ 1228】Grandpa&#39;s Estate 凸包

找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 1003 #define read(x) x = getint() using namespace std; inline int getint() { int k = 0, fh = 1; char c

【POJ 2750】 Potted Flower(线段树套dp)

[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   Accepted: 1739 Description The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrou

【POJ 2480】Longge&#39;s problem(欧拉函数)

题意 求$ \sum_{i=1}^n gcd(i,n) $ 给定 $n(1\le n\le 2^{32}) $. 链接 分析 用欧拉函数$φ(x)$求1到x-1有几个和x互质的数. gcd(i,n)必定是n的一个约数.若p是n的约数,那么gcd(i,n)==p的有$φ(n/p)$个数,因为要使gcd(i,n)==p,i/p和n/p必须是互质的.那么就是求i/p和n/p互质的i在[1,n]里有几个,就等价于,1/p,2/p,...,n/p里面有几个和n/p互质,即φ(n/p). 求和的话,约数为p