http://acm.hdu.edu.cn/showproblem.php?pid=1010

翻译:有只狗被困了,S是起点,D是门,W是墙不能走,‘ . ’是可以走的路,走一次就会在1秒内坍塌,也就是不能停留也不能走回头路,门只在第T秒打开,问是否能逃命?

解题:一开始以为是三维bfs,但是地图上的时间维度是错误的,因为走过一次地面坍塌,只能有一个时间维度。百度找了居然一个bfs都没有,不得不用dfs,无限超时,甚至记忆化搜索也超时。见识到了高端的剪枝。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; bool vis[][];
char a[][];
int d[][]={ {-,}, {,}, {,-}, {,} };///上下左右 int n,m,T,cnt;
bool flag;
struct node
{
int x;
int y;
int t;
};
node door,start; void dfs(int x,int y,int t)///当前用了多少时间
{
if( x==door.x && y==door.y && t==door.t )
{
flag=true;
return ;
} for(int i=;i<;i++)
{
int cha=T-t-abs(door.x-x)-abs(door.y-y);///核心代码 int xx=x+d[i][];
int yy=y+d[i][];
int tt=t+;
if( !flag && xx>= && xx<n && yy>= && yy<m && !vis[xx][yy] && (a[xx][yy]=='.'||a[xx][yy]=='D') && cha>= && cha%== )
{
vis[xx][yy]=true;
dfs(xx,yy,tt);
vis[xx][yy]=false;
}
} } int main()///HDU1010
{
while(scanf("%d%d%d",&n,&m,&T) && (n+m+T) )
{
memset(a,,sizeof(a));
memset(vis,false,sizeof(vis));
flag=false; for(int i=;i<n;i++)
scanf("%s",a[i]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if( a[i][j]=='D' )///终点
door.x=i,door.y=j,door.t=T;
if( a[i][j]=='S' )///起点
{
start.x=i,start.y=j,start.t=;
vis[ start.x ][ start.y ] = true; }
}
dfs( start.x,start.y, );
if(flag)
printf("YES\n");
else
printf("NO\n");
} return ;
}

dfs内,x和y表示当前坐标,t表示当前花了多少时间。

当前步数到终点的最短距离:abs(door.x-x)+abs(door.y-y)

还需要花的时间:T-t

走最短路的话还剩余的时间:cha = T - t - abs(door.x-x) + abs(door.y-y)

在没有障碍的情况下,cha至少要等于0,如果有障碍,cha要大于0,剪枝剪掉那些 就算没障碍走最短路也走不到的情况。

奇偶剪枝:

举例:

0 1 0 1

0 0

0 1

1 0 1 0

假设红色1要走到蓝色1,最短只需要走2步,但是现在有4步,必须绕远,绕过粉色1和粉色0走到蓝色1,如果是3步,怎么走都走不到。

hdu1010-Tempter of the Bone-(dfs+奇偶剪枝)的更多相关文章

  1. 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 ...

  2. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  4. 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 ...

  5. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  6. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  7. hdu Tempter of the Bone (奇偶剪枝)

    学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...

  8. hdu1010 Tempter of the Bone(深搜+剪枝问题)

    Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...

  9. hdu1010Tempter of the Bone(dfs+奇偶剪枝)

    题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...

  10. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

随机推荐

  1. Springboot Actuator之十:actuator中的audit包

    前言这篇文章我们来分析一下org.springframework.boot.actuate.security,org.springframework.boot.actuate.audit中的代码,这2 ...

  2. ElasticSearch 线程池类型分析之 ResizableBlockingQueue

    ElasticSearch 线程池类型分析之 ResizableBlockingQueue 在上一篇文章 ElasticSearch 线程池类型分析之 ExecutorScalingQueue的末尾, ...

  3. 更改collation批处理

    DECLARE @zcreate_index_sql NVARCHAR(max); SET @zcreate_index_sql = N''; SELECT @zcreate_index_sql = ...

  4. - Charles 简介 总结 HTTP 抓包 代理 MD

    目录 目录 Charles 简介 Charles 破解工具 界面介绍 主菜单 会话右键菜单 两种显示模式 内容区域 抓包 HTTP 抓包 HTTPS 抓包 HTTPS 抓包原理 请求重定向 Map r ...

  5. Java之四大元注解@Target、@Retention、@Documented、@Inherited

    什么叫做元注解??   ==>用于注解[注释]的注解就叫做元注解 注解叫做:元数据,标签,注释           元注解[数据]--->注解--->标记代码 1.@Target : ...

  6. 动软软件 生成 实体类模板(EnterpriseFrameWork框架)

    1.废话不多说,直接上效果图 . 2 .动软模板代码 <#@ template language="c#" HostSpecific="True" #&g ...

  7. Golang 是否有必要内存对齐?

    原文:https://ms2008.github.io/2019/08/01/golang-memory-alignment/ 内存模型 Posted by ms2008 on August 1, 2 ...

  8. 3DESC加密算法

    3DESC 请求参数和响应参数全采用3des加密规则,由于我是用.NET对接的,而第三方是Java开发的,所以两种程序之间采用的算法有一点差异,java的3des加密采用的是"DESede/ ...

  9. 2019 云和数据java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.云和数据等公司offer,岗位是Java后端开发,因为发展原因最终选择去了云和数据,入职一年时间了,也成为了面 ...

  10. java 判断虚拟网卡物理网卡

    读取注册表方式,jregistrykey.jar与jregistrykey.dll.通过“characteristics”值确定虚拟网卡还是物理网卡.该值在注册表的位置HKEY_LOCAL_MACHI ...