hdu1010 Tempter of the Bone---DFS+奇偶剪枝
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1010
题目描述:
根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过,问:在指定的时间,是否能到达'门'的位置。注意:路不可以重复经过,时间也要刚好是 t ,不能少.
思路:
此处不能用BFS,因为时间要恰好为t,还是得用DFS,不过需要剪枝才能过。
奇偶剪枝:
从一个点到达另外一个点的最短路径长度(时间)可以根据两点坐标求出,路径长度(非最短)与最短路径的长度同奇偶,它们的差一定是偶数!举个例子,就像两个偶数的差差是偶数,两个个数的差也是偶数.
此处还有一个剪枝:
设墙的数目为wall,如果wall + t >= n * m,一定到达不了,因为大于号显然成立,这里主要讨论等号的情况。
比如下图
3 3 1
SDX
XXX
XXX
只需要一步就可以到达,此时wall = 7, t = 1,wall + t < n * m,有可行解,这是由于有S,D的存在,所以如果有可行解,wall的数目一定会比n*m-t要小,等于的话是没有可行解的。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
const int INF = <<;
const int maxn = ;
int T, cases;
int n, m, k, x1, y1, x2, y2;
char Map[][];
int dir[][] = {,,,,-,,,-};
bool dfs(int x, int y, int time)//到x1, y1点,花费时间time
{
if(x < || x >= n || y < || y >= m || time > k)return false;
//剪枝
if(time == k && x == x2 && y == y2)return true;
int mintime = k - time - abs(x - x2) - abs(y - y2);
if(mintime < )return false;//最优性剪枝,当前沿着最优路径走,还是会超过k秒,返回假
if(mintime & )return false;//奇偶剪枝,相减的时间是奇数的话,在k秒的时候一定到不了终点 for(int i = ; i < ; i++)
{
int xx = x + dir[i][];
int yy = y + dir[i][];
if(Map[xx][yy] != 'X')
{
Map[xx][yy] = 'X';
if(dfs(xx, yy, time + ))return true;
Map[xx][yy] = '.';
}
}
return false;
}
int main()
{
while(cin >> n >> m >> k && (n + m + k))
{
int wall = ;
for(int i = ; i < n; i++)
{
cin >> Map[i];
for(int j = ; j < m; j++)
{
if(Map[i][j] == 'S')x1 = i, y1 = j;
else if(Map[i][j] == 'D')x2 = i, y2 = j;
else if(Map[i][j] == 'X')wall++;
}
}
if(wall + k >= n * m)
///这里是一个特别好的剪枝,如果墙的数目+步数>=n*m,一定不可能完成任务
///重点考虑等号,除了墙还有'S'和'D',墙的数目最多为n*m-k-1(距离,比如k = 1时,S和D相邻,其他都是墙,此时墙的数目最多为n*m-2,满足上述式子)
{
printf("NO\n");
continue;
}
Map[x1][y1] = 'X';//先设置成X表示该点不能再经过
if(dfs(x1, y1, ))printf("YES\n");
else printf("NO\n");
}
}
hdu1010 Tempter of the Bone---DFS+奇偶剪枝的更多相关文章
- hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- hdu Tempter of the Bone (奇偶剪枝)
学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...
- hdu1010 Tempter of the Bone(深搜+剪枝问题)
Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...
- hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
随机推荐
- java安全框架shiro(一)
第一个简单的案例 ,通过读取.ini文件的方式模拟登陆, 1.通过Factory工厂的getInstance()方法来获取SecurityManager的实例,实例化Factory需要一个ini文件的 ...
- Day3---------Linux操作系统
---恢复内容开始--- 网络基础和DOS命令 一.网络分类 1.地理位置 1).局域网(LAN) 2).城域网(MAN) 3).广域网(WAN) 2.传输介质 1).有线网 2).光纤网 3).无线 ...
- 【数据库】mysql数据库索引
文章归属:http://feiyan.info/16.html,我想自己去写了,但是发现此君总结的非常详细.直接搬过来了 关于MySQL索引的好处,如果正确合理设计并且使用索引的MySQL是一辆兰博基 ...
- 国内可用的Internet时间同步服务器地址(NTP时间服务器)
不知道什么鬼我这系统自带的Internet时间同步服务器地址居然不可用,终端ping系统自带服务器两个居然都不通???难道时间服务器也和谐么? 好在阿里云提供了7个NTP时间服务器也就是Interne ...
- 将 Shiro 作为应用的权限基础 四:shiro的配置说明
Apache Shiro的配置主要分为四部分: SecurityManager的配置 URL过滤器的配置 静态用户配置 静态角色配置 其中,由于用户.角色一般由后台进行操作的动态数据,比如通过@Req ...
- [BZOJ 1297][SCOI2009]迷路
1297: [SCOI2009]迷路 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1418 Solved: 1017[Submit][Status ...
- unittest自动化使用HTMLTestRunner的中文编码问题
1.使用unittest自动化测试框架,使用HTMLTestRunner生成测试报告,中文乱码问题! 如图 2.解决方法: 第一步:先在自己的测试脚本中添加 import sys reload(sys ...
- c语言程序设计第6周编程作业一(分解质因数)
分解质因数 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. 现在,你的程 ...
- Alpha第十天
Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- C语言博客作业—嵌套循环
一.PTA实验作业 题目1:7-4 换硬币 1. 本题PTA提交列表 2. 设计思路 (1)定义整型变量money表示待换的零钱总额,p5表示5分硬币的数量,p2表示2分硬币的数量,p1表示1分硬币的 ...