POJ 1222 EXTENDED LIGHTS OUT (熄灯问题)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8417 | Accepted: 5441 |
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
所以,枚举第一行按钮是否按下的所有状态就行。
解法一: 使用for循环枚举(只适用于循环层数已知的情况)
#include<iostream>
#include<cstring>
#include<stdio.h> using namespace std; int n;
int p = ;
int orig_maze[][]; //原始的迷宫
int maze[][]; //每次循环后都要将maze重新还原为orig_maze
int result[][] = { }; void opposite(int x, int y)
{
if (x >= && x < && y >= && y < )
{
if (maze[x][y] == )
maze[x][y] = ;
else
maze[x][y] = ;
}
} void press(int x, int y)
{
opposite(x, y);
opposite(x + , y);
opposite(x - , y);
opposite(x, y + );
opposite(x, y - );
} bool judge()
{
for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
{
if (maze[i][j] == )
return false;
}
return true; } void solve()
{
for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
{
cin >> orig_maze[i][j];
maze[i][j] = orig_maze[i][j];
} //枚举第一行按下的按钮
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
{
for (int i = ; i <= ; ++i)
{ //按第一行
if (result[][i] == )
press(, i);
} //由第一行算出后面四行
for (int i = ; i <= ; ++i)
{
int k = i - ; //k表示i的上一行
for (int j = ; j <= ; ++j)
{
if (maze[k][j] == )
{
result[i][j] = ; //将其置为1表示按下
press(i, j);
}
else
result[i][j] = ; //没按下一定要置零,因为以前循环时置的1还保留在此
}
} if (judge() == true)
{
printf("PUZZLE #%d\n", p++);
for (int i = ; i < ; ++i)
{
int j;
for (j = ; j < ; ++j)
cout << result[i][j] << ' ';
cout << result[i][j] << endl; }
return ;
} memcpy(maze, orig_maze, sizeof(orig_maze)); //将迷宫还原
}
} int main()
{
int n;
cin >> n; while (n--)
{
solve();
} return ;
}
解法二: 使用dfs枚举(适用于循环层数未知的情况)
#include<iostream>
#include<cstring>
#include<stdio.h> using namespace std; int m, n;
int orig_maze[][]; //原始的迷宫
int maze[][]; //每次循环后都要将maze重新还原为orig_maze
int result[][] = { };
int book[][]; // 使用book保存第一行的所有枚举情况
int index = ;
int p = ; void opposite(int x, int y)
{
if (x >= && x < m && y >= && y < n)
{
if (maze[x][y] == )
maze[x][y] = ;
else
maze[x][y] = ;
}
} void press(int x, int y)
{
opposite(x, y);
opposite(x + , y);
opposite(x - , y);
opposite(x, y + );
opposite(x, y - );
} bool judge()
{
for (int i = ; i < m; ++i)
for (int j = ; j < n; ++j)
{
if (maze[i][j] == )
return false;
}
return true; } void dfs(int step)
{
if (step == n)
{
for (int i = ; i < n; ++i)
book[index][i] = result[][i]; // 使用book保存第一行的所有枚举情况
index++;
return;
} for (int i = ; i <= ; ++i)
{
result[][step] = i;
dfs(step + );
}
} void g()
{ for (int i = ; i < m; ++i)
for (int j = ; j < n; ++j)
{
cin >> orig_maze[i][j];
maze[i][j] = orig_maze[i][j];
} dfs(); for (int t = ; t < index; ++t)
{
for (int t1 = ; t1 < n; ++t1)
{
result[][t1] = book[t][t1];
} for (int i = ; i < n; ++i)
{ //按第一行
if (result[][i] == )
press(, i);
} //由第一行算出后面m-1行
for (int i = ; i < m; ++i)
{
int k = i - ; //k表示i的上一行
for (int j = ; j < n; ++j)
{
if (maze[k][j] == )
{
result[i][j] = ; //将其置为1表示按下
press(i, j);
}
else
result[i][j] = ; //没按下一定要置零,因为以前循环时置的1还保留在此
}
} if (judge() == true)
{
printf("PUZZLE #%d\n", p++);
for (int i = ; i < m; ++i)
{
int j;
for (j = ; j < n - ; ++j)
cout << result[i][j] << ' ';
cout << result[i][j] << endl; }
return ;
}
memcpy(maze, orig_maze, sizeof(orig_maze)); //将迷宫还原
} } int main()
{
m = ;
n = ;
int k;
cin >> k;
while(k--)
{
g();
} return ;
}
POJ 1222 EXTENDED LIGHTS OUT (熄灯问题)的更多相关文章
- POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)
POJ 1222 EXTENDED LIGHTS OUT 今天真是完美的一天,这是我在poj上的100A,留个纪念,马上就要期中考试了,可能后面几周刷题就没这么快了,不管怎样,为下一个200A奋斗, ...
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元解异或方程组)
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10835 Accepted: 6 ...
- Poj 1222 EXTENDED LIGHTS OUT
题目大意:给你一个5*6的格子,每个格子中有灯(亮着1,暗着0),每次你可以把一个暗的点亮(或者亮的熄灭)然后它上下左右的灯也会跟着变化.最后让你把所有的灯熄灭,问你应该改变哪些灯. 首先我们可以发现 ...
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元)题解
题意:5*6的格子,你翻一个地方,那么这个地方和上下左右的格子都会翻面,要求把所有为1的格子翻成0,输出一个5*6的矩阵,把要翻的赋值1,不翻的0,每个格子只翻1次 思路:poj 1222 高斯消元详 ...
- POJ 1222 EXTENDED LIGHTS OUT(反转)
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12616 Accepted: 8 ...
- OpenJudge 2811 熄灯问题 / Poj 1222 EXTENDED LIGHTS OUT
1.链接地址: http://bailian.openjudge.cn/practice/2811 http://poj.org/problem?id=1222 2.题目: 总时间限制: 1000ms ...
- poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
http://poj.org/problem?id=1222 题意:给一个确定的5*6放入矩阵.每一个格子都有一个开关和一盏灯,0表示灯没亮,1表示灯亮着.让你输出一个5*6的矩阵ans[i][j], ...
- 【高斯消元】Poj 1222:EXTENDED LIGHTS OUT
Description In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each ...
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元)
[题目链接] http://poj.org/problem?id=1222 [题目大意] 给出一个6*5的矩阵,由0和1构成,要求将其全部变成0,每个格子和周围的四个格子联动,就是说,如果一个格子变了 ...
随机推荐
- php 汉字首字母和全拼
<?php/** *+------------------------------------------------------ * PHP 汉字转拼音 *+----------------- ...
- 关于如何实现Android透明状态栏的总结
开门见山. 原来做的效果,如下图(顶部有一条明显的橙色状态栏): a1.gif 改过之后(顶部状态栏是透明的): p2.gif 我发现网上写的一些文章,不够简洁明了,我整理了一下,复制粘贴一下 ...
- RedHat Linux关闭seLinux命令
Redhat使用了SELinux来增强安全,关闭的办法为: 1. 永久有效 修改 /etc/selinux/config 文件中的 SELINUX="" 为 disabled ,然 ...
- IOS 静态库 和 动态库
库从本质上市一中可执行的二进制格式,可以被载入内存中执行 iOS 中的静态库有 .a 和 .framework 两种形式; 动态库有 .dylib 和 .framework 两种, 后来 ...
- MVVM 简介
转:https://objccn.io/issue-13-1/ 所以,MVVM 到底是什么?与其专注于说明 MVVM 的来历,不如让我们看一个典型的 iOS 是如何构建的,并从那里了解 MVVM: 我 ...
- oracle 根据一个时间段获取这个时间段内所有月份、天数、日期
注:本文来源于< oracle 根据一个时间段获取这个时间段内所有月份.天数.日期 > 获取月份列表: SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014-10 ...
- Windows7上完全卸载Oracle 12c操作步骤
注:本文来源于:< Windows7上完全卸载Oracle 12c操作步骤 > 1.关闭Oracle所有的服务,按[win+R]运行[services.msc]找到所有Oracle开头的 ...
- Oracle 高水位说明和释放表空间,加快表的查询速度
高水位的介绍 数据库运行了一段时间,经过一些列的删除.插入.更改操作有些表的高水位线就有可能和实际的表存储数据的情况相差特别多,为了提高检索该表的效率,建议对这些表进行收缩: 查找高水位线的表 查找表 ...
- Vmware Workstation _linux yum 仓库搭建
0:检查 vm虚拟机光盘是否已经连接 1. 检测yum 仓库是否已经配置好 [root@oracle ~]# yum list all 如果输入这条指令可以正确显示出rpm 包的列表,则说明yum 仓 ...
- web的分页方法
web分页的三种方式,闲来无事总结一下. 1.使用前端表格插件进行分页 例如用bootstrap的拓展table组件,注意设置其分页属性时设置为"client", 即是 sideP ...