POJ1222(SummerTrainingDay01-E)

时间:2022-12-14 20:14:47

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. 
POJ1222(SummerTrainingDay01-E)
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. 
POJ1222(SummerTrainingDay01-E)
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. 二进制枚举例题&vert;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(高斯消元法解异或方程组&plus;开关问题)

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

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

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

  5. poj1222 EXTENDED LIGHTS OUT 高斯消元&vert;&vert;枚举

    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. &lbrack;POJ1222&rsqb;EXTENDED LIGHTS OUT(高斯消元,异或方程组)

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

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

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

  9. &lbrack;Gauss&rsqb;POJ1222 EXTENDED LIGHTS OUT

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

  10. bzoj2466&comma;poj1222

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

随机推荐

  1. C&num;向sql server数据表添加数据源代码

    HoverTree解决方案 学习C#.NET,Sql Server,WinForm等的解决方案. 本文链接http://hovertree.com/h/bjaf/0jteg8cv.htm 使用的技术. ...

  2. linux下apache-tomcat的安装

    一.JDK安装 1.安装JDK软件包 本例使用的JDK安装包为jdk-6u19-linux-x64.bin,该包是一个编译好的二进制可执行程序包,只需要执行即可安装. 首先进入存放JDK安装包的目录( ...

  3. Thml 小插件8 天气插件定制

    网址:http://www.tianqi.com/plugin/

  4. ACM网络镜像赛

    参加了东大,南理的网络镜像赛,分别答对了两道题和三道题.感觉自己完全被虐了.发现了很多自己应该去掌握的知识,找到了自己的差距.容斥原理,博弈论,等等,这将是我努力的方向. 在跟大神同学和学姐交流的过程 ...

  5. 国内值得关注的官方API集合

    项目地址:https://github.com/marktony/Awesome_API 本页仅收集国内部分官方API,如需查看其他版本,请点击这里. 目录 笔记 出行 词典 电商 地图 电影 后端云 ...

  6. kafka分布式消息队列介绍以及集群安装

    简介 首先简单说下对kafka的理解: 1.kafka是一个分布式的消息缓存系统: 2.kafka集群中的服务器节点都被称作broker 3.kafka的客户端分为:一是producer(消息生产者) ...

  7. types&period;go

    } type ChannelStatsList []*ChannelStats func (c ChannelStatsList) Len() int { return len(c) } func ( ...

  8. vimium使用

    vimium使用 chrome下面的vimium插件已经慕名已久,迟迟没有做尝试,今天在家有空就熟悉了一下vimium,感觉还是棒棒的.记录一下一些使用心得. 常用按钮 j 向上滚动 k 向下滚动 d ...

  9. golang 使用os&sol;exec配合context实现的超时机制

    在使用golang开发中,调用外部可执行程序通过exec包是我们常用的方式.如何控制超时请见如下样例: var ( Timeout = 3 * time.Second ) func Command(n ...

  10. &lbrack;hyperscan&rsqb;&lbrack;pkg-config&rsqb; hyperscan 从0到1路线图

    经过一系列的研究学习,知识储备之后,终于,可以开始研究hyperscan了. [knowledge][模式匹配] 字符匹配/模式匹配 正则表达式 自动机 [knowledge][perl][pcre] ...