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. Html 常见meta

    html 的meta标签对网页渲染及SEO搜索引擎起着不可忽视的作用.详细的写法一段时间不写,容易忘,所以整理了一下,方便需要时查看. <!DOCTYPE html> <!-- 使用 ...

  2. spring cloud 学习(6) - zuul 微服务网关

    微服务架构体系中,通常一个业务系统会有很多的微服务,比如:OrderService.ProductService.UserService...,为了让调用更简单,一般会在这些服务前端再封装一层,类似下 ...

  3. 【转】iOS中属性与成员变量的区别

    [转载自并整理 http://blog.csdn.net/itianyi/article/details/8618128] 一.类Class中的属性property 在ios第一版中,我们为输出口同时 ...

  4. java操作数据库的基本方法

    此次开发工具为eclipse,才有的数据库驱动是mysql-connector-java-5.1.8-bin.jar 第一步,在eclipse的工程目录中引用mysql驱动 驱动下载地址:https: ...

  5. [原创]K8 DNN密码解密工具(DotNetNuke Password Decrypt)

    工具: K8_DNN_Password_Decrypt编译: VS2012  C# (.NET Framework v2.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http: ...

  6. POJ 2590

    #include<iostream> #include<algorithm> #define MAXN 1000000 using namespace std; unsigne ...

  7. 03-01 Java运算符

    (1)算术运算符 A:+,-,*,/,%,++,-- B:+的用法 a:加法 b:正号 c:字符串连接符 C:/和%的区别 数据做除法操作的时候,/取得是商,%取得是余数 D:++和--的用法 a:他 ...

  8. 学习推荐-Postgresql学习手册

    Postgresql之旅: http://www.cnblogs.com/stephen-liu74/archive/2012/06/08/2315679.html postgresql逻辑结构+权限 ...

  9. IntelliJ IDEA导入多个eclipse项目到同一个workspace下

    IntelliJ IDEA 与eclipse在新建项目上工作区的叫法略有不同,区别见下图. 我们在eclipse都是在新建的workspace目录下新建我们的项目,但是在IDEA中没有workspac ...

  10. 全球各大邮件SMTP服务限制

    常用免费SMTP服务的发送限制,转贴 GMail 免费邮箱 http://gmail.google.comSMTP 服务器:smtp.gmail.com策略:发送延时 0,每天发送量限制 50(GMa ...