POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 22057 | Accepted: 8521 | Special Judge |
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+--
----
----
-+--
Sample Output
6
1 1
1 3
1 4
4 1
4 3
4 4 同1753一样的代码,但是这题有一点不是很明白,就是没有Impossible的可能,
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int handle[][];
int flag,step;
int r[],c[];
int all_open()
{
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
if(!handle[i][j])
return false;
}
return true;
}
void change(int row, int col)
{
handle[row][col] = !handle[row][col]; //没写这个DFS里面就是死循环了
for(int i = ; i <= ; i++)
{
handle[row][i] = !handle[row][i];
handle[i][col] = !handle[i][col];
}
}
void dfs(int row, int col, int deep)
{
if(deep == step)
{
flag = all_open();
return;
}
if(flag || row > )
return; change(row, col);
r[deep] = row;
c[deep] = col;
if(col < )
{
dfs(row, col + , deep + );
}
else
{
dfs(row + , , deep + );
}
change(row, col);
if(col < )
{
dfs(row, col + , deep);
}
else
{
dfs(row + , , deep);
}
return;
}
int main()
{
char s[];
while(scanf("%s", s) != EOF)
{
memset(handle, , sizeof(handle));
for(int i = ; i < ; i++)
if(s[i] == '-')
handle[][i + ] = ;
for(int i = ; i <= ; i++)
{
scanf("%s", s);
for(int j = ; j < ; j++)
if(s[j] == '-')
handle[i][j + ] = ;
} flag = ;
for(step = ; step <= ; step++)
{
dfs(, , );
if(flag)
break;
}
if(flag)
{
printf("%d\n", step);
for(int i = ; i < step; i++)
printf("%d %d\n", r[i],c[i]);
}
}
return ;
}
POJ2965The Pilots Brothers' refrigerator(枚举+DFS)的更多相关文章
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
- POJ2965The Pilots Brothers' refrigerator
http://poj.org/problem?id=2965 这个题的话,一开始也不会做,旁边的人说用BFS,后来去网上看了众大神的思路,瞬间觉得用BFS挺简单易:因为要让一个“+”变为“-”,只要将 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- The Pilots Brothers' refrigerator
2965 he Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
随机推荐
- 获取一个字符串Hello world中world首次出现的位置
获取一个字符串Hello world中world首次出现的位置 var str=“Hello world!” document.write(str.indexOf("world") ...
- 域策略禁用usb
文档及模板可在 http://pan.baidu.com/s/1qYTcjTy 下载 pro_usb_users.adm 此模板可禁用到 指定盘符,针对用户策略 pro_usb_computers ...
- [py]chr ord
http://www.xuebuyuan.com/2067536.html 也可以help(ord)
- ZooKeeper学习第三期---Zookeeper命令操作
一.Zookeeper的四字命令 Zookeeper支持某些特定的四字命令字母与其的交互.他们大多数是查询命令,用来获取Zookeeper服务的当前状态及相关信息.用户在客户端可以通过telnet或n ...
- python数字图像处理(8):对比度与亮度调整
图像亮度与对比度的调整,是放在skimage包的exposure模块里面 1.gamma调整 原理:I=Ig 对原图像的像素,进行幂运算,得到新的像素值.公式中的g就是gamma值. 如果gamma& ...
- Uedit的快捷键
Key1 自动换行_CTRL + W 这个已经不是什么新奇的功能了,就连你们最不喜欢的notepad都有了这个功能.说来也奇怪,编辑器为什么都带有这个功能呢?谁愿意自己的编辑器带有水平滚动条啊 ...
- HDU5336-XYZ and Drops-模拟
模拟水珠那个游戏. 小水珠超过边界会消失. 会有两个水珠同时到达一个size=4大水珠的情况.要移动完统一爆炸 #include <vector> #include <cstdio& ...
- 20145222黄亚奇《Java程序设计》第2周学习总结
教材学习内容总结 类型: 整数:short(占2字节).int(4).long(8). 浮点数:float(4).double(8) 字符:char(2) 布尔:boolean类型表示true与fal ...
- JS的解析机制
JS的解析机制,是JS的又一大重点知识点,在面试题中更经常出现,今天就来唠唠他们的原理.首先呢,我们在我们伟大的浏览器中,有个叫做JS解析器的东西,它专门用来读取JS,执行JS.一般情况是存在作用域就 ...
- HTTP之手机抓包工具篇
简介 现在手机移动互联网时代 手机app 运用 如日冲天.自然手机app的问题排除也是头疼,明明自己测试 上线的接口正常 到了手机app就不行.怎么办呢?别急,现在有好多手机抓包工具啦! 1. Cha ...