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,每个格子和周围的四个格子联动,就是说,如果一个格子变了 ...
随机推荐
- SqlBulkCopy 之 Received an invalid column length from the bcp client for colid 5.
SqlBulkCopy 批量复制报错: Received an invalid column length from the bcp client for colid 5. 翻译:从bcp客户端收到一 ...
- 【转】nvidia-smi 命令解读
nvidia-smi是linux下用来查看GPU使用情况的命令.具体的参数信息详见 原文:http://blog.csdn.net/sallyxyl1993/article/details/62220 ...
- 2018年Android的保活方案效果统计
一.常见保活方案 1.监听广播:监听全局的静态广播,比如时间更新的广播.开机广播.解锁屏.网络状态.解锁加锁亮屏暗屏(3.1版本),高版本需要应用开机后运行一次才能监听这些系统广播,目前此方案失效.可 ...
- 使用FreeRTOS进行性能和运行时分析
在MCU on Eclipse网站上看到Erich Styger在2月25日发的博文,一篇关于使用FreeRTOS进行性能和运行分析的文章,本人觉得很有启发,特将其翻译过来以备参考.当然限于个人水平, ...
- Confluence 6 反向跟踪
当反向跟踪(Trackback )被启用后,在任何你链接到可用启用自动发现功能的外部页面中,Confluence 将会自动发送一个方向跟踪 ping,这个 ping 能通知链接的页面有了内容改变. C ...
- spring cloud 使用ribbon简单处理客户端负载均衡
假如我们的multiple服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改 ...
- springboot多环境(dev、test、prod)配置
propertiest配置格式在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如: ...
- springboot拦截器HandlerInterceptor详解
Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器). HandlerInterceptor 的功能跟过滤器类似,但 ...
- ActiveSync的Settings命令
在[MS-ASCMD]中,Settings命令的功能这样定义的: The Settings command also sends device information to ...
- Ubuntu 进入、退出命令行的快捷键
进入: Ctrl+Alt+F1 退出: Ctrl+Alt+F7(或者 Alt+F7) 进入命令行窗口:Ctrl+Alt+T