POJ1222(SummerTrainingDay01-E)
EXTENDED LIGHTS OUT
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 11078 | Accepted: 7074 |
Description

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
Output
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)的更多相关文章
- 二进制枚举例题|poj1222,poj3279,poj1753
poj1222,poj3279,poj1753 听说还有 POJ1681-画家问题 POJ1166-拨钟问题 POJ1054-讨厌的青蛙
- POJ1222、POJ3279、POJ1753--Flip
POJ1222-EXTENDED LIGHTS OUT POJ3279-Fliptile POJ1753-Flip Game 为什么将着三个题放一起讲呢?因为只要搞明白了其中一点,就可以一次3ac了- ...
- poj1222(高斯消元法解异或方程组+开关问题)
题目链接:https://vjudge.net/problem/POJ-1222 题意:给定一个5×6的01矩阵,改变一个点的状态时它上下左右包括它自己的状态都会翻转,因为翻转2次等价与没有翻转,那么 ...
- 高斯消元几道入门题总结POJ1222&&POJ1681&&POJ1830&&POJ2065&&POJ3185
最近在搞高斯消元,反正这些题要么是我击败了它们,要么就是这些题把我给击败了.现在高斯消元专题部分还有很多题,先把几道很简单的入门题总结一下吧. 专题:http://acm.hust.edu.cn/vj ...
- poj1222 EXTENDED LIGHTS OUT 高斯消元||枚举
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8481 Accepted: 5479 Description In an ...
- poj1222 EXTENDED LIGHTS OUT
设输入矩阵为A,输出矩阵为B,目标矩阵为C(零矩阵). 方便起见,矩阵行列下标均从1开始. 考虑A矩阵元素a(i,j),B矩阵中与其相邻的元素 b(i,j),b(i - 1, j),b(i + 1,j ...
- [POJ1222]EXTENDED LIGHTS OUT(高斯消元,异或方程组)
题目链接:http://poj.org/problem?id=1222 题意:开关是四连通的,每按一个就会翻转自己以及附近的四个格(假如有).问需要翻转几个,使他们都变成关. 把每一个灯看作一个未知量 ...
- POJ1222 高斯消元法解抑或方程
第一次学怎么用高斯消元法解抑或方程组,思想其实很简单,方法可以看下面的链接:http://blog.csdn.net/zhuichao001/article/details/5440843 有了这种思 ...
- [Gauss]POJ1222 EXTENDED LIGHTS OUT
题意:给一个5*6的矩阵 1代表该位置的灯亮着, 0代表该位置的灯没亮 按某个位置的开关,可以同时改变 该位置 以及 该位置上方.下方.左方.右方, 共五个位置的灯的开.关(1->0, 0-&g ...
- bzoj2466,poj1222
都是简单的异或高斯消元 由于bzoj2466要求解得最小和,所以我们最后还要穷举自由元取最优解 type node=record po,next:longint; end; . ...
随机推荐
- 多个SpringMVC项目配置统一管理(来自于springCloud的统一配置思路)
因公司项目分多个系统进行开发,而系统架构几乎完全一样,所以同样的配置文件会存在不同的系统中 当其中的某些配置需要修改时,就需要依次把所有系统中相关的配置都修改掉 纯耗时且没技术含量的体力活 所以借鉴S ...
- Liferay7 BPM门户开发之2: BPMN 2.0 规范入门 (Activiti BPMN extensions)
Liferay最大的问题是BPM弱,如果做企业开发,BPM必不可少,所以直入主题,做个BPMN2入门. 本文参考地址:http://activiti.org/userguide/index.html# ...
- VS2015 WPF Prism Xaml Designer error
Ref: http://wiki.tk2kpdn.com/build-error-prism5-interactionrequesttrigger-with-vs2015/ gacutil -i &q ...
- mysql基础知识(2)
十 一.计算字段 计算字段通常需要使用 AS 来取别名,否则输出的时候字段名为计算表达式 select col1*col2 as col12 from mytable concat() 用于连接两个字 ...
- WTF小程序之<web-view>
叨叨两句 昨天爬了一下午坑才出来的我向大家问好
- wnmp(windows+nginx+mysql+php)环境搭建和配置
要求 必备知识 熟悉基本编程环境搭建. 运行环境 windows 7(64位); nginx-1.4.7;MySQL Server 5.5php-5.4.39-nts 下载地址 环境下载 Nginx是 ...
- 轻量级web富文本框——wangEditor使用手册(3)——如何自定义配置菜单 demo
最新版wangEditor: 配置说明:http://www.wangeditor.com/doc.html demo演示:http://www.wangeditor.com/wangEditor/d ...
- rabbitmq 生产环境配置
目录 一 rabbitmq 生产部署 1.1 rabbitmq.conf 1.2 advanced.config 1.3 rabbitmq-env.conf 1.4 在生产环境中不适用的策略. 一 r ...
- epoll 触发模式
Edge Triggered (ET):边缘触发只有数据到来,才触发,不管缓存区中是否还有数据.Level Triggered (LT):水平触发只要有数据都会触发. LT(level trigger ...
- Lambda 遍历
遍历列表元素 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...