EXTENDED LIGHTS OUT

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11078   Accepted: 7074

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

 
 //2017-08-01
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std; bool grid[][], tmp[][];
int n, m, ans[], res[]; void flip(int x, int y)
{
tmp[x][y] = !tmp[x][y];
if(x->=)tmp[x-][y] = !tmp[x-][y];
if(y->=)tmp[x][y-] = !tmp[x][y-];
tmp[x+][y] = !tmp[x+][y];
tmp[x][y+] = !tmp[x][y+];
} void solve()
{
int penn, minflip = 0x3f3f3f3f;
bool fg = false;
for(int i = ; i < (<<m); i++)
{
for(int x = ; x < n; x++)
for(int y = ; y < m; y++)
tmp[x][y] = grid[x][y];
for(int y = ; y < m; y++)
if(i&(<<y))
flip(, m--y);
ans[] = i;
for(int x = ; x < n; x++){
penn = ;
for(int y = ; y < m; y++){
if(tmp[x-][y]){
flip(x, y);
penn += (<<(m--y));
}
}
ans[x] = penn;
}
bool ok = true;
for(int j = ; j < m; j++)
if(tmp[n-][j])
ok = false;
if(ok){
fg = true;
int cnt = ;
for(int j = ; j < n; j++){
for(int pos = ; pos < m; pos++)
if(ans[j]&(<<(m--pos)))cnt++;
}
if(cnt < minflip){
minflip = cnt;
for(int k = ; k < n; k++)
res[k] = ans[k];
}
}
}
if(!fg)cout<<"IMPOSSIBLE"<<endl;
else{
for(int j = ; j < n; j++){
for(int pos = ; pos < m; pos++)
if(pos == m-)cout<<(res[j]&(<<(m--pos))?:)<<endl;
else cout<<(res[j]&(<<(m--pos))?:)<<" ";
}
}
} int main(){
int T;
cin>>T;
for(int kase = ; kase <= T; kase++){
n = ;
m = ;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
cin>>grid[i][j];
cout<<"PUZZLE #"<<kase<<endl;;
solve();
} return ;
}

POJ1222(SummerTrainingDay01-E)的更多相关文章

  1. 二进制枚举例题|poj1222,poj3279,poj1753

    poj1222,poj3279,poj1753 听说还有 POJ1681-画家问题 POJ1166-拨钟问题 POJ1054-讨厌的青蛙

  2. POJ1222、POJ3279、POJ1753--Flip

    POJ1222-EXTENDED LIGHTS OUT POJ3279-Fliptile POJ1753-Flip Game 为什么将着三个题放一起讲呢?因为只要搞明白了其中一点,就可以一次3ac了- ...

  3. poj1222(高斯消元法解异或方程组+开关问题)

    题目链接:https://vjudge.net/problem/POJ-1222 题意:给定一个5×6的01矩阵,改变一个点的状态时它上下左右包括它自己的状态都会翻转,因为翻转2次等价与没有翻转,那么 ...

  4. 高斯消元几道入门题总结POJ1222&&POJ1681&&POJ1830&&POJ2065&&POJ3185

    最近在搞高斯消元,反正这些题要么是我击败了它们,要么就是这些题把我给击败了.现在高斯消元专题部分还有很多题,先把几道很简单的入门题总结一下吧. 专题:http://acm.hust.edu.cn/vj ...

  5. poj1222 EXTENDED LIGHTS OUT 高斯消元||枚举

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8481   Accepted: 5479 Description In an ...

  6. poj1222 EXTENDED LIGHTS OUT

    设输入矩阵为A,输出矩阵为B,目标矩阵为C(零矩阵). 方便起见,矩阵行列下标均从1开始. 考虑A矩阵元素a(i,j),B矩阵中与其相邻的元素 b(i,j),b(i - 1, j),b(i + 1,j ...

  7. [POJ1222]EXTENDED LIGHTS OUT(高斯消元,异或方程组)

    题目链接:http://poj.org/problem?id=1222 题意:开关是四连通的,每按一个就会翻转自己以及附近的四个格(假如有).问需要翻转几个,使他们都变成关. 把每一个灯看作一个未知量 ...

  8. POJ1222 高斯消元法解抑或方程

    第一次学怎么用高斯消元法解抑或方程组,思想其实很简单,方法可以看下面的链接:http://blog.csdn.net/zhuichao001/article/details/5440843 有了这种思 ...

  9. [Gauss]POJ1222 EXTENDED LIGHTS OUT

    题意:给一个5*6的矩阵 1代表该位置的灯亮着, 0代表该位置的灯没亮 按某个位置的开关,可以同时改变 该位置 以及 该位置上方.下方.左方.右方, 共五个位置的灯的开.关(1->0, 0-&g ...

  10. bzoj2466,poj1222

    都是简单的异或高斯消元 由于bzoj2466要求解得最小和,所以我们最后还要穷举自由元取最优解 type node=record        po,next:longint;      end; . ...

随机推荐

  1. CentOS 7修改yum源为阿里源

    1.备份本地源 1 # mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak 2.获取阿里yum源配置 ...

  2. zookeeper安装小记

    做了5个节点,但是查看状态的时候,发现 ./zkServer.sh statusJMX enabled by defaultUsing config: /opt/zookeeper/bin/../co ...

  3. while循环中使用scanf函数

    妈的,这scanf函数学了快10年了,怎么还会出现莫名其妙的问题?看下面的代码(VS2012环境下运行): #define _CRT_SECURE_NO_WARNINGS #include<st ...

  4. win10+Theano+GPU

    1. cuda + cudnn 首先还是要先安装GPU库,具体和caffe安装中一样. 2. Theano 为防止下载速度慢,配置清华镜像 conda config --add channels ht ...

  5. 玄武短信接口和移动MAS短信接口的API封装

    直接上代码,关键点: 133行的敏感词过滤 176行的6位扩展码写入 using System; using System.Collections.Generic; using System.Linq ...

  6. POJ 2719

    #include<iostream> #include<stdio.h> using namespace std; ]; int _pow(int m,int n); int ...

  7. POJ 2578

    #include<iostream> #include<stdio.h> #include<vector> using namespace std; int mai ...

  8. 判断一个类是否为另一个类的实例 instanceof关键字和isAssignableFrom方法的区别

    Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only ...

  9. Tornado初探

    Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了能有效 ...

  10. PHP:session无法使用

    今天在将一套程序放到其他服务器上执行的时候,发现后台的登录验证码不管输入正确与否,总是显示: 验证码输入有误 接着就开始debug了. 因为正确的验证码结果已经经过加密之后保存在了session中,所 ...