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 ...
随机推荐
- 【C#】实现按Windows排序方式排序
很多人或许都遇到过类似的问题,C#读取的文件列表默认是按ASCII排序的,这样会出现一个问题就是10会排在2的前面. 那么是否可以解决呢,答案是肯定的.虽然这个是很早之前遇到的问题,这次突然想起来,就 ...
- Python中的sort()方法使用基础
一.基本形式 sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)itera ...
- GeoServer 常见问题总结
Geoserver安装环境 Geoserver在部署发布服务时,经常会遇到如下问题,现总结如下: 1.忘记了GeoServer Web Admin Page的登陆用户名和密码怎么办? 存储位置:C:\ ...
- js计算两个日期相隔几小时几分钟?
var dt1 = "2009-11-5 10:30" var dt2 = "2009-11-8 9:20" function ge ...
- linux查看系统信息命令
本文转载自江一<linux查看系统信息命令> # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /p ...
- ViewConfiguration.getScaledTouchSlop () 用法
getScaledTouchSlop是一个距离,表示滑动的时候,手的移动要大于这个距离才开始移动控件.如果小于这个距离就不触发移动控件,如viewpager就是用这个距离来判断用户是否翻页 ViewC ...
- html:关于表单功能的学习
比如我在某jsp页面中写了如下表单: <form action="/MavenWeb/TestFormPost" method="get"> & ...
- LeetCode 笔记25 Candy (艰难的调试)
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 20135220谈愈敏Blog8_进程的切换和系统的一般执行过程
进程的切换和系统的一般执行过程 谈愈敏 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-100002 ...
- rem详解及使用方法
好像有一段时间没有写博客了……今天刚好总结一下rem的使用方法 首先,先说一个常识,浏览器的默认字体高都是16px.步入正题-----〉 兼容性: 目前,IE9+,Firefox.Chrome.Saf ...