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 ...
随机推荐
- 21SpringMvc_异步发送表单数据到Bean,并响应JSON文本返回(这篇可能是最重要的一篇了)
这篇文章实现三个功能:1.在jsp页面点击一个按钮,然后跳转到Action,在Action中把Emp(int id ,String salary,Data data)这个实体变成JSON格式返回到页面 ...
- ubuntu 16.04 mysql 相关
如何彻底卸载某一版本的数据库 彻底删除ubuntu下的mysql: 1.删除mysql的数据文件 sudo rm /var/lib/mysql/ -R 2.删除mqsql的配置文件 sudo rm / ...
- Fedora 12 环境搭建
又来折腾发行版了. 这一回是Fedora12,搞的挺艰难的 下载了Fedora-12-i386-DVD.iso,无论使用ultraiso还是dd都无法安装. 后来下载了一个ImageWriter.ex ...
- error C3872: "0xa0": 此字符不允许在标识符中使用
整理:这是因为直接复制代码的问题.0xa0是十六进制数,换成十进制就是160,表示汉字的开始. 解决办法:在报错的代码行检查两边的空格,用英文输入法的空格替换掉. 万恶的网络,万恶的word,这些无厘 ...
- 利用window.name+iframe跨域获取数据详解
详解 前文提到用jsonp的方式来跨域获取数据,本文为大家介绍下如何利用window.name+iframe跨域获取数据. 首先我们要简单了解下window.name和iframe的相关知识.ifra ...
- apply、call、callee、caller初步了解
在javascript中这四货通常一起出现介绍,楼主记忆力实在是太差经常忘记用法,故记此文. apply和call apply和call是函数原型的一个方法,调用者的类型必须是函数.官方解释:应用某一 ...
- 仿照easy-ui并改进的表单验证
概述 easy-ui有自身的一套表单验证,扩展方便,但默认下也存在一些弱点,比如多规则验证.后台验证.远程异步验证等,这些功能要解决起来是比较吃力的.我仿照它的样式,写了一套前端表单验证的validB ...
- Sublime Text 3使用参考手册
什么是Sublime Text? Sublime Text 是一个代码编辑器(Sublime Text 2是收费软件,但可以无限期试用),也是HTML和散文先进的文本编辑器.Sublime Text是 ...
- javaWeb开发模式
1.发展历程 2.模式分析 JSP+JavaBean模式适合开发业务逻辑不太复杂的web服务程序.这种模式下,JavaBean用于封装业务数据,JSP即负责处理用户请求,又显示数据(JSP编写业务逻辑 ...
- [Aaronyang] 写给自己的WPF4.5 笔记[2依赖属性]
人生的意义不在于拿一手好牌,而在于打好一手坏牌 --Aaronyang的博客(www.ayjs.net)-www.8mi.me =============时隔两年后再看WPF========== 因为 ...