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,每个格子和周围的四个格子联动,就是说,如果一个格子变了 ...
随机推荐
- 06 元祖 字典 集合set
元组 定义: ti=() print(ti,type(ti)) 参数:for可以循环的对象(可迭代对象) t2=tuple(") # ('1', '2', '3') <class 't ...
- Java 调用翻译软件实现英文文档翻译
前言: 因最近要进行OCP的考试准备.看着大堆英文文档确实有些疼痛.又因文档内容有点大,又需要逐一去翻译 又很费时费力.于是 百度了一番,找到一些 可以使用Java来调用百度翻译软件的API( 注:( ...
- Confluence 6 workbox 通知包含了什么
当一个用户在 Confluence 中进行下面的操作的时候,workbox 将会显示为通知: 分享(Shares)你的页面或者博客页面. 提及(Mentions)你的页面,博客页面,回复或者任务. 你 ...
- try? try! try do catch try 使用详解
当一个使用一个方法发现后面 throws 说明可能会抛出异常 需要try 进行处理 1 try? 如果解析成功就有值 否则返回nil (推荐) 2 try! 如果解析成功就有值 否则直接崩溃 ...
- Android “Command” from work summary
总结一下Android中的命令. 一.adb 与 shell ADB的全称为Android Debug Bridge(调试桥).是一个适用命令行工具,用来与模拟器实例或链接的Android设备进行通信 ...
- ionic3 启动白屏处理
重点就就三点 1.设置不自动隐藏splashscreen页面,设置最多延迟10s再关闭,这样可以不显示启动前的白画面 <preference name="AutoHideSplashS ...
- 10,关于在vs2017中的netcore项目使用BundlerMinifier 配置问题
查阅资料 https://github.com/madskristensen/BundlerMinifier/issues/230 今天下载了vs2017 rc 创建了个netcore的web项目,突 ...
- python冒泡排序法
- java易错题----静态方法的调用
class A{ public static String s="A.s"; } class B extends A{ public static String s="B ...
- 在lnmp环境下,将原来的PHP7.0升级到PHP7.2
基础环境: 系统:centos6.8 环境:lnmp 停止PHP7.0的版本,在做如下操作: 1.下载php-7.2.6.tar.bz2软件包放在/opt 路径下 mkdir /usr/local ...