POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
题目:http://poj.org/problem?id=2965
来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#problem/B
题意:
----
----
----状态
算法:
思路:
code:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std; int map[5][5];
int x[20]; // 临时路径
int y[20]; int ansX[20]; // 最终路径
int ansY[20];
int ans = 33; void build()
{
char c;
memset(map, 0, sizeof(map));
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cin>>c;
if(c == '-') map[i][j] = 1; //open
else map[i][j] = 0;
}
}
} void flip(int s)
{
int x1 = s/4; // 行
int y1 = s%4; // 列 for(int i = 0; i < 4; i++)
{
map[i][y1] = !map[i][y1];
map[x1][i] = !map[x1][i];
}
map[x1][y1] = !map[x1][y1];
} bool complete() // 判断是否达到目标
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
if(map[i][j] == 0) return false;
}
return true;
} void dfs(int s, int b) // 遍历到第 s 个, 翻转了 0个
{
if(complete())
{
if(ans > b)
{
ans = b;
for(int i = 1; i <= ans; i++)
{
ansX[i] = x[i];
ansY[i] = y[i];
}
}
return;
} if(s >= 16) return; dfs(s+1, b); //不管第 s 个,直接往下找 flip(s); //翻转第 s 个了再往下找
x[b+1] = s/4+1; //临时记录路径【注意】
y[b+1] = s%4+1; dfs(s+1, b+1); //翻转第 s 个了再找下一个 flip(s); //回溯
} int main()
{
build();
dfs(0, 0);
printf("%d\n", ans);
for(int i = 1; i <= ans; i++)
{
printf("%d %d\n", ansX[i], ansY[i]);
}
return 0;
}
POJ 2965 The Pilots Brothers' refrigerator【枚举+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 (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的矩阵,因 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22057 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
随机推荐
- MySQL的id生成策略
1 自增 CREATE TABLE `test` ( `id` ) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAUL ...
- spring-hadoop-samples
官方的spring-hadoop-samples的demo 写的还是挺好的,值得学习. 官网地址: http://projects.spring.io/spring-hadoop/#quick-sta ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- redux VS mobx (装饰器配合使用)
前言:redux和mobx都是状态管理器,避免父级到子级再到子子级嵌套单向数据流,可以逻辑清晰的管理更新共享数据.(刷新页面redux储蓄数据即消失) 配置使用装饰器(使用高阶函数包装你的组件): n ...
- vi 替换操作
举例一: ,$s/// 从第一行到最后一行 147都替换为150 举例二: 例:替换当前文件中所有old为new :%s/old/new/g #%表示替换说有行,g表示替换一行中所有匹配点. 举例三: ...
- svn解决与优化帮助
1.问题的出现 解决方案: 最后一行不能子目录. 启动的时候也是要根目录的.svnserve -d -r /home/svn/repos [不能是svnserve -d -r /home/svn/re ...
- Android_编程规范与经常使用技巧
一.Android编码规范 1.java代码中不出现中文,最多凝视中能够出现中文 2.局部变量命名.静态成员变量命名 仅仅能包括字母,单词首字母出第一个外.都为大写,其它字母都为小写 3.常量命名 仅 ...
- jmeter压测-负载配置
jmeter 压测 一般压测的时间是10-15分钟 TPS:服务端每秒钟处理的请求数 越大越好 响应时间 :越短越好 并发用户数 也就是多少并发 指标给你:tps要达到多少 响应时间要达到多少 并 ...
- ASP.NET CORE RAZOR :向 Razor 页面应用添加模型
本文来自:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/model 在本部分中将添加用于管理数据库中的电影的类. ...
- js中box和box()的区别
window.onload = function(){ var input = document.getElementByTagName('input')[0]; input.onclick = bo ...