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的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
随机推荐
- Mycat 分片规则详解--范围取模分片
实现方式:该算法先进行范围分片,计算出分片组,组内在取模 优点:综合了范围分片和取模分片的优点,分片组内使用取模可以保证组内的数据分布比较均匀,分片组之间采用范围分片可以兼顾范围分片的特点,事先规划好 ...
- Spring Boot 定时任务的使用
@Configuration @EnableScheduling public class ScheduleConfig { private final Logger logger = LoggerF ...
- 简单爬虫 -- 以爬取NASA AOD数据(TIFF文件)为例
目录: 网站分析 爬取下载链接 爬取TIFF图片 1.网站分析 主页面:https://neo.sci.gsfc.nasa.gov/view.php?datasetId=MYDAL2_M_AER_OD ...
- 基于hi-nginx的web开发(python篇)——cookie和会话管理
hi-nginx通过redis管理会话. 要开启管理,需要做三件事. 第一件开启userid: userid on; userid_name SESSIONID; userid_domain loca ...
- (转)Android 仿订单出票效果 (附DEMO)
之前我下载了BaseAnimation 开源库(BaseAnimation是基于开源的APP,致力于收集各种动画效果) BaseAnimation 转载的链接:http://blog.csdn.net ...
- Python下载图片小程序
欢迎大侠们指正批评 思路: 1.引入相关的python文件(import re import urllib) 2.读取对应网页的html文件(使用 urllib) def getHtml(url): ...
- SQLSERVER2012的分页新功能
SQLSERVER2012的分页新功能 简介 SQL Server 2012中在Order By子句之后新增了OFFSET和FETCH子句来限制输出的行数从而达到了分页效果.相比较SQL Server ...
- JAVA线程概念
一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程中可以启动多个线程.比如在 ...
- Swift -欢迎界面1页, 延长启动图片的显示时间(LaunchImage)
转自:http://www.hangge.com/blog/cache/detail_1238.html http://www.hangge.com/blog/cache/detail_672.htm ...
- mahony互补滤波器C编程
//gx...分别为重力加速度在三个轴向的分力 由加速度计测得 //ax...分别为角速度在三个轴向的角速度 由陀螺仪测得 //最后得到最终滤波完毕的x.y.z方向的角度值(°) void IMUup ...