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,每个格子和周围的四个格子联动,就是说,如果一个格子变了 ...
随机推荐
- CM5.15安装kafka
cm主节点执行: [root@dip001 kafka]#ll KAFKA-.jar KAFKA---el7.parcel KAFKA---el7.parcel.sha1 manifest.json ...
- 关于STM32 __IO 的变量定义
这个_IO 是指静态 这个 _IO 是指静态 volatile uint32_t 是指32位的无符号整形变量uint32_t 是指32位的无符号整形变量: 搞stm32这么久了,经常看到stm32里面 ...
- 一篇文章让你了解Android各个版本的历程
2008年--至今 Android 1.5(Cupcake纸杯蛋糕): 智能虚拟键盘:使用widgets实现桌面个性化:在线文件夹(Live Folder)快速浏览在线数据:视频录制和分享:图片上传: ...
- swift 学习- 15 -- 构造过程 01
// 构造过程 是使用类,结构体 或 枚举类型的实例之前的准备过程, // 在新实例可用前必须执行这个过程, 具体操作包括 设置实例中每个存储型属性的初始值 和 执行其他必须的设置 或 初始化工作 / ...
- nginx实践(二)之静态资源web服务(浏览器缓存场景)
配置语法-expires
- 一.定时任务详解https://i.cnblogs.com/EditPosts.aspx
定时任务的详解 h每小时运行 d每天运行 w每周运行 m每个月运行 中毒时查看系统定时任务是否有病毒的脚本. crontab -l 查看用户的定时任务 crontab -e 进入编辑界面增加 ...
- 动手动脑——JAVA语法基础
EnumTest.java public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Si ...
- python提取文件中的方法名称
#提取文件中的方法名称 # -*- coding:utf-8 -*- def Query_Method(filepath): file = open(filepath,'r',encoding= 'U ...
- MyEclipes相关配置
0. MyEclipes10 相关下载资源(私人珍藏版) 链接:http://pan.baidu.com/s/1eSIdObS密码:0cjy 1. myEclipes连接Tomcat http://w ...
- ffmpeg切割视频
using System.Diagnostics; public static void carveVideo() { var inputpath = @"d:\1.mp4"; v ...