POJ1222熄灯问题【位运算+枚举】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14231 | Accepted: 8817 |
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
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
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
– 每个按钮的位置上有一盏灯
– 当按下一个按钮后, 该按钮以及周围位置(上边, 下边, 左边, 右边)的灯都会改变状态

– 如果灯原来是点亮的, 就会被熄灭
– 如果灯原来是熄灭的, 则会被点亮
• 在矩阵角上的按钮改变3盏灯的状态
• 在矩阵边上的按钮改变4盏灯的状态
• 其他的按钮改变5盏灯的状态
与一盏灯毗邻的多个按钮被按下时,一个操作会抵消另一次操作的结果
给定矩阵中每盏灯的初始状态,求一种按按钮方案,使得所有的灯都熄灭
输入:
– 第一行是一个正整数N, 表示需要解决的案例数
– 每个案例由5行组成, 每一行包括6个数字
– 这些数字以空格隔开, 可以是0或1
– 0 表示灯的初始状态是熄灭的
– 1 表示灯的初始状态是点亮的
输出:
– 对每个案例, 首先输出一行,
输出字符串 “PUZZLE #m”, 其中m是该案例的序号
– 接着按照该案例的输入格式输出5行
• 1 表示需要把对应的按钮按下
• 0 表示不需要按对应的按钮
• 每个数字以一个空格隔开
思路:
如何减少内存:这题灯一共五行六列,char占一个字节也就是有8位,可以用一位表示一列,所以每一行用一个char字符就能保存一行灯或开关的状态了,开一个char[5]就能保存所有灯或开关的状态了。
如何减少枚举次数:不用每行每列都枚举一遍,可以只枚举第一行开关所有的情况,共有2的6次方=64种,第一行的开关确定,剩下每一行的开关也就确定了。原因是因为题目要求是每个灯都要熄灭,
如果第一行的开关按完后,只会影响第一行和第二行,假设第一行还有未熄灭的灯,那么第二行的开关肯定就是要熄灭第一行的灯,依次类推,所以我们只需要枚举第一行的情况,最后看看按照第一行开关的按法,第五行的灯是不是全熄灭了就行。
#include<iostream>
#include<string.h>
using namespace std;
char orilight[5];//记录原始灯的情况 ,一共五行,每行用一个char字符表示原始灯的状态
char changelight[5];//灯的变化情况
char result[5];//最终开关方案
int T;//测试案例个数
void setBit(char &c,int i,int v)//设置c的第i位为v
{
if(v)
{
c |= (1<<i);//设为1
}
else
c &= ~(1<<i);//设为0 ,~:取反
}
int getBit(char c,int i)//获得c的第i位
{
int t = (c>>i) & 1;//右移i位后和1与
return t;
}
void flipBit(char &c,int i)//对c的第i位取反
{
c ^= (1<<i);//翻转某一位:与1异或
}
int main()
{
cin >> T;
for(int x = 1;x<=T;++x)
{
//输入测试数据
for(int i = 0;i<5;++i)
for(int j = 0;j<6;++j)
{
int t;
cin >> t;
setBit(orilight[i],j,t);
} for(int n = 0;n<64;++n)
{
memcpy(changelight,orilight,sizeof(orilight));//每测试一种情况前都需要先把原始灯复制到改变灯数组里,调试改变灯数组里的 情况
int switchs = n;
for(int i = 0;i<5;++i)
{
result[i] = switchs;
for(int j = 0;j<6;++j)
{
int r = getBit(switchs,j); if(r)
{
if(j>0)
flipBit(changelight[i],j-1);
flipBit(changelight[i],j);
if(j<5)
flipBit(changelight[i],j+1); } }
if(i<4)//把i+1正下面那个灯取反
changelight[i+1] ^= result[i];
switchs = changelight[i];//下一行的开关等于这一行亮的灯 }
if(changelight[4]==0)//如果第五行灯全熄灭了,说明这次方案是正确的
{
cout << "PUZZLE #"<<x<<endl;
for(int i = 0;i<5;++i)
{
for(int j = 0;j<6;++j)
{
cout << getBit(result[i],j);
if(j!=5)
cout <<" ";
}
cout << endl;
}
break;
}
} }
return 0;
}
POJ1222熄灯问题【位运算+枚举】的更多相关文章
- 枚举进行位运算 枚举组合z
枚举进行位运算--枚举组合 public enum MyEnum { MyEnum1 = , //0x1 MyEnum2 = << , //0x2 MyEnum3 = << , ...
- POJ 1753 位运算+枚举
题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...
- 在C#中对枚举进行位运算--枚举组合
由于枚举的基础类型类型为基本的数值类型,支持位运算,因此可以使用一个值表示多个枚举的组合,在定义枚举时需要指定枚举数为2的幂指数方便进行位运算,即枚举数为1,2,4,8…,或1,1<<1, ...
- hdu 1882 Strange Billboard(位运算+枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=1882 感觉非常不错的一道题. 给一个n*m(1<=n,m<=16)的矩阵,每一个格子都有黑白两面,当 ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- HDU 6186 CS Course【前后缀位运算枚举/线段树】
[前后缀枚举] #include<cstdio> #include<string> #include<cstdlib> #include<cmath> ...
- 【bzoj2073】【[POI2004]PRZ】位运算枚举子集的特技
(上不了p站我要死了) Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍在桥上的人都不能超过一 ...
- poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
http://poj.org/problem?id=1222 题意:给一个确定的5*6放入矩阵.每一个格子都有一个开关和一盏灯,0表示灯没亮,1表示灯亮着.让你输出一个5*6的矩阵ans[i][j], ...
- POJ 2531 Network Saboteur 位运算子集枚举
题目: http://poj.org/problem?id=2531 这个题虽然是个最大割问题,但是分到dfs里了,因为节点数较少.. 我试着位运算枚举了一下,开始超时了,剪了下枝,1079MS过了. ...
随机推荐
- Python生成器实现斐波那契数列
比如,斐波那契数列:1,1,2,3,5,8,13,21,34.... 用列表生成式写不出来,但是我们可以用函数把它打印出来: def fib(number): n, a, b = 0, 0, 1 wh ...
- poj 3281 Dining (最大网络流)
题目链接: http://poj.org/problem?id=3281 题目大意: 有n头牛,f种食物,d种饮料,第i头牛喜欢fi种食物和di种饮料,每种食物或者饮料被一头牛选中后,就不能被其他的牛 ...
- [USACO 2011 Nov Gold] Above the Median【逆序对】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...
- 转 PHP Cookies
cookie 常用于识别用户. 什么是 Cookie? cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie. ...
- jmeter(三)参数传递
[一]参数化 录制脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者想更好的模拟多个用户来登录系统. 这个时候就需要对用户名和密码进行参数化,使每个虚拟用户都使用不 ...
- 462 Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等 II
给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000.例如:输入:[1,2,3]输出:2说明:只有两个动作是必 ...
- Rsync 12种故障排查及思路
Rsync 故障排查整理 Rsync服务常见问题汇总讲解: ====================================================================== ...
- hadoop-2.4.1集群搭建及zookeeper管理
准备 1.1修改主机名,设置IP与主机名的映射 [root@xuegod74 ~]# vim /etc/hosts 192.168.1.73 xuegod73 192.168.1.74 xuegod7 ...
- 动态添加出来append的元素加事件
$("body").on("click","#box span",function(){里面执行的东西}) span 是动态添加出来的
- .NET框架概述
.NET战略目标: 任何时候(when),任何地方(where),使用任何工具(what)都能通过.NET的服务获得网络上的任何信息. .NET优势: 1.提供了一个面向对象的编程环境,完全支持面向对 ...