Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8481   Accepted: 5479

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

 
两种做法,一开始的做法是枚举,即枚举第0行的按不按的情况,这样第1行按的情况由第0行来决定,比如第a[0][3]=1,那么就要按[1][3]才能使得[0][3]=0,最后看第4行是不是全为0就可以了
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int a[][], rcd[][], ans[][];
int dir[][] = {{, }, {, }, {, -}, {-, }};
void push(int x, int y) {
ans[x][y] = ;
rcd[x][y] ^= ;
for(int k = ; k < ; ++k) {
int xx = x + dir[k][];
int yy = y + dir[k][];
if(xx < || xx >= || yy < || yy > ) continue;
rcd[xx][yy] ^= ;
}
}
bool check(int cur) {
for(int i = ; i < ; ++i) if(( << i) & cur) {
push(, i);
}
for(int i = ; i < ; ++i) {
for(int j = ; j < ; ++j)
if(rcd[i - ][j]) push(i, j);
}
for(int i = ; i < ; ++i) if(rcd[][i]) return false;
return true;
}
void solve() {
for(int i = ; i < ( << ); ++i) {
for(int r = ; r < ; ++r)
for(int c = ; c < ; ++c)
rcd[r][c] = a[r][c];
memset(ans, , sizeof ans);
if(check(i)) break;
}
}
void out() {
for(int i = ; i < ; ++i) {
for(int j = ; j < ; ++j)
if(j == ) printf("%d\n", ans[i][j]);
else printf("%d ", ans[i][j]);
}
}
int main() {
int _, cas = ; scanf("%d", &_);
while(_ --) {
for(int i = ; i < ; ++i)
for(int j = ; j < ; ++j)
scanf("%d", &a[i][j]);
printf("PUZZLE #%d\n", cas++);
solve();
out();
}
}

学到了另一种做法是高斯消元,可以形成(行数*列数)个方程,未知数的个数也是(行数*列数),即按下(i,j),相当于原矩阵异或x*Aij,x取0或1(不按或按)

Aij是按下位置(i,j)时所影响的位置代表的矩阵,比如3*3的矩阵,按下(1,1),那么A11 = 0 1 0, 按下(0,1), A01 = 1 1 1

                                        1 1 1           0 1 0

                                        0 1 0           0 0 0

设原矩阵为M

对于每个位置(i,j),我们考虑按或不按,那么就有M ^ x(i,j)*Aij = O, O表示0矩阵,等式两边同时异或M,那么有x(i,j)*Aij = M,两个矩阵相等,即为每个每个位置的元素对应相等,那么就可以建立(i*j)个方程组

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = ;
int a[MAXN][MAXN], b[MAXN][MAXN], x[MAXN];
int equ, var;
void Gauss() {
int i, j, k, col, maxr, temp;
for(i = ; i <= var; ++i) x[i] = ;
for(k = , col = ; k < equ && col < var; k++, col++) {
maxr = k;
for(int i = k + ; i < equ; ++i)
if(abs(a[i][col]) > abs(a[maxr][col]))
maxr = i; if(a[maxr][col] == ) { k--; continue; }
if(k != maxr) {
for(j = k; j < var + ; ++j)
swap(a[k][j], a[maxr][j]);
}
for(i = k + ; i < equ; ++i) {
if(a[i][col] != ) {
// LCM = lcm(abs(a[i][col]), abs(a[k][col]));
// ta = LCM / abs(a[i][col]);
// tb = LCM / abs(a[k][col]);
// if(a[i][col] * a[k][col] < 0) tb = -tb;
for(j = col; j < var + ; ++j)
a[i][j] = a[i][j] ^ a[k][j];
}
}
}
for(i = var - ; i >= ; --i) {
temp = a[i][var];
for(j = i + ; j < var; ++j)
temp ^= (a[i][j] * x[j]);
x[i] = temp;
}
}
int vis[][];
int dir[][] = {{, }, {, }, {-, }, {, -} };
void get(int x, int y)
memset(vis, , sizeof vis);
vis[x][y] = ;
for(int i = ; i < ; ++i) {
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx < || xx >= || yy < || yy > ) continue;
vis[xx][yy] = ;
}
for(int i = ; i < ; ++i) {
for(int j = ; j <= ; ++j) printf("%d ", vis[i][j]);
puts("");
}
puts(""); void debug() {
for(int i = ; i < ; ++i) {
for(int j = ; j <= ; ++j)
printf("%d ", a[i][j]);
puts("");
}
}
void init() {
memset(a, , sizeof a);
equ = var = ;
int cur = ;
for(int i = ; i < ; ++i)
for(int j = ; j <= ; ++j)
a[cur++][] = b[i][j];
// debug();
cur = ;
for(int i = ; i < ; ++i) {
for(int j = ; j <= ; ++j) {
get(i, j);
int k = ;
for(int r = ; r < ; ++r)
for(int c = ; c <= ; ++c)
a[k++][cur] = vis[r][c];
cur++;
}
}
// debug();
} void out() {
for(int i = ; i < ; ++i) {
if((i + ) % == ) printf("%d\n", x[i]);
else printf("%d ", x[i]);
}
}
int main() {
int _, cas = ; scanf("%d", &_);
while(_ --) {
for(int i = ; i < ; ++i)
for(int j = ; j <= ; ++j)
scanf("%d", &b[i][j]);
init();
printf("PUZZLE #%d\n", cas++);
Gauss();
// debug();
out();
}
}

poj1222 EXTENDED LIGHTS OUT 高斯消元||枚举的更多相关文章

  1. POJ1222 EXTENDED LIGHTS OUT 高斯消元 XOR方程组

    http://poj.org/problem?id=1222 在学校oj用搜索写了一次,这次写高斯消元,haoi现场裸xor方程消元没写出来,真实zz. #include<iostream> ...

  2. [poj1222]EXTENDED LIGHTS OUT(高斯消元)

    题意:每个灯开启会使自身和周围的灯反转,要使全图的灯灭掉,判断灯开的位置. 解题关键:二进制高斯消元模板题. 复杂度:$O({n^3})$ #include<cstdio> #includ ...

  3. POJ 1222 EXTENDED LIGHTS OUT (高斯消元)

    题目链接 题意:5*6矩阵中有30个灯,操作一个灯,周围的上下左右四个灯会发生相应变化 即由灭变亮,由亮变灭,如何操作使灯全灭? 题解:这个问题是很经典的高斯消元问题.同一个按钮最多只能被按一次,因为 ...

  4. EXTENDED LIGHTS OUT (高斯消元)

    In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual ...

  5. POJ 1222 EXTENDED LIGHTS OUT [高斯消元XOR]

    题意: $5*6$网格里有一些灯告诉你一开始开关状态,按一盏灯会改变它及其上下左右的状态,问最后全熄灭需要按那些灯,保证有解 经典问题 一盏灯最多会被按一次,并且有很明显的异或性质 一个灯作为一个方程 ...

  6. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...

  7. BZOJ 1770: [Usaco2009 Nov]lights 燈( 高斯消元 )

    高斯消元解xor方程组...暴搜自由元+最优性剪枝 -------------------------------------------------------------------------- ...

  8. POJ 1681 Painter's Problem(高斯消元+枚举自由变元)

    http://poj.org/problem?id=1681 题意:有一块只有黄白颜色的n*n的板子,每次刷一块格子时,上下左右都会改变颜色,求最少刷几次可以使得全部变成黄色. 思路: 这道题目也就是 ...

  9. BZOJ1770:[USACO]lights 燈(高斯消元,DFS)

    Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...

随机推荐

  1. iOS 本人必装插件

    本人觉得比较好用也实用的Xcode插件记录: 1. Alcatraz   插件通过它来管理 :    https://github.com/alcatraz/Alcatraz.git 2. Cocoa ...

  2. grep(Global Regular Expression Print)

    .grep -iwr --color 'hellp' /home/weblogic/demo 或者 grep -iw --color 'hellp' /home/weblogic/demo/* (-i ...

  3. “无法更新EntitySet“*****”,因为它有一个DefiningQuery,而元素中没有支持当前操作的元素”问题的解决方法

    百思不得其解,最后发现 1:实体中的表必须有主键(数据库中的表必须有主键),如果没有,会有这样的提示 2:主键设置好后,运行还是会出现类似问题,那就一个郁闷 1):方法一:先从EF中删除刚设置主键的模 ...

  4. Linuxc:创建与监控多个子进程

    #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <signal ...

  5. bootstratp图标的使用

    bootstratp作为一个优秀的前端框架,最近使用了其中的Glyphicon Halflings的字体图标.起初一直显示不出来,后面通过搜索相关资料直到成功显示,在此做一些总结,方便后面复习. 1. ...

  6. HTML5 – 4.canvas

    1.绘制矩形 2.绘制圆形 3.绘制文字 4.保存文件 什么是 Canvas? HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成. < ...

  7. Linux环境下段错误的产生原因及调试方法小结

    转载自http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...

  8. Pyqt 中__init__(self,parent==None) parent理解

    参考: 在PyQt中,所有class都是从QObject派生而来,QWidget对象就可以有一个parent.这种parent-child关系主要用于两个方面: 没有parent的QWidget类被认 ...

  9. Power BI Q&A终于在圣诞前夕盼到

    相信跟所有的数据分析师们一样,赶上年底和年初都是非常忙的时候,即使赶上哪天运气好不加班每天回到家吃完饭恨不得倒在床上就美美的睡上一觉.本人也是如此,正直疲惫之际,尹相志在微博上把我一圈,说Power ...

  10. jquery判断当前设备是手机还是电脑并跳转

    <script type="text/javascript"> var commonURL = 'http://xxxx.com/'; function mobile_ ...