poj 2965 枚举+DFS
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 25343 | Accepted: 9786 | 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
思路:因为每个位置最多翻一次,所以最多翻16次,用枚举来做。(代码是看别人的,有一部分还不能完全理解。。)
#include "cstdio"
#include "algorithm"
#include "cstring"
char map[][];
int m[][];
typedef struct {
int s,t;
}road;
road a[];
int k,ans;
int P(){
for(int i=;i<=;i++){
for(int j=;j<=;j++){
if(m[i][j]==){
return ;
}
}
}
return ;
}
void G(int x,int y){
m[x][y]^=;
for(int i=;i<=;i++){
m[x][i]^=;
}
for(int i=;i<=;i++){
m[i][y]^=;
}
}
void dfs(int x,int y,int step) {
// G(x, y);
if (step == ans) {
k = P();//
return;
}
if(k||x>=){//不太理解这里为什么要判断k是否为1,个人觉得只要当ans=step时在dfs外判断即可??
return;
}
G(x, y);
if(y<){
dfs(x,y+,step+);
a[step].s=x;
a[step].t=y;
}
else {
dfs(x+,,step+);
a[step].s=x;
a[step].t=y;
}
G(x,y);
if(y<){
dfs(x,y+,step);
}
else {
dfs(x+,,step);
} }
int main(){
k=;
for(int i=;i<=;i++){
scanf("%s",map[i]);
for(int j=;j<;j++){
if(map[i][j]=='-'){
m[i][j+]=;
}
else if(map[i][j]=='+'){
m[i][j+]=;
}
}
}
for(int i=;i<=;i++){
ans=i;
dfs(,,);
if(k){
printf("%d\n",ans);
break;
}
}
for(int i=;i<ans;i++){
printf("%d %d\n",a[i].s,a[i].t);
}
return ;
}
poj 2965 枚举+DFS的更多相关文章
- POJ 1753 (枚举+DFS)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40632 Accepted: 17647 Descr ...
- POJ 3050 枚举+dfs+set判重
思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- 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
id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析
题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...
随机推荐
- python面试题——基础篇(80题)
1.为什么学习Python? Python是一门优秀的综合语言, Python的宗旨是简明.优雅.强大,在人工智能.云计算.金融分析.大数据开发.WEB开发.自动化运维.测试等方向应用广泛 2.通过什 ...
- 详解__FILE__与$_SERVER['SCRIPT_FILENAME']的区别
废话不多说 直接上测试代码: <?php //引入的是ceshi4文件夹下的ceshi4.php; require_once './ceshi4/ceshi4.php'; 下面是ceshi4文件 ...
- canvas制作倒计时效果
- Android SQLite数据库之事务的学习
SQLite是Android系统内置的一款轻量级的关系型数据库,它的运算速度非常快,占用资源很少,通常只需要几百K的内存就足够了.SQLite不仅支持标准的SQL语法,还遵循了数据库的ACID事务. ...
- Quartz Cron表达式的二三事
最近在解决产品上的一个需求,就是定期生成报告(Report),我们叫做Scheduled Report. 原理:UI获取用户输入的时间信息,后台使用Spring框架设置定时任务,这里定时任务用的就是 ...
- Struts2_默认Action
配置Struts2默认跳转的Action <package name="default" namespace="/" extends="stru ...
- redis在Windows下以后台服务一键搭建哨兵(主从复制)模式(多机)
redis在Windows下以后台服务一键搭建哨兵(主从复制)模式(多机) 一.概述 此教程介绍如何在windows系统中多个服务器之间,布置redis哨兵模式(主从复制),同时要以后台服务的模式运行 ...
- Spark job执行流程消息图
Spark job执行流程消息图 1.介绍
- 关于硬盘分区使用exFat格式的优势及劣势(含摘抄)
优势 可以设置最大32M的簇: 不记录日志. 劣势 无法使用windows的“文件共享”: 通过近期某个文件数量密级任务的测试发现,在大量文件的处理性能上,NTFS比exFAT文件系统的性能高出不少. ...
- ARM是CPU体系结构
https://zhidao.baidu.com/question/680620766286548532.html ARM是一种使用精简指令(RISC)的CPU,有别于英特尔的复杂指令(CISC) x ...