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. 第二节:EF Core的常规“增删改”及状态的变化

    一. 整体说明 1. 本节用到的表 2. 状态说明补充 ①.Detached: 游离的状态,与数据库没有什么交涉,比如新new一个实体,状态就是Detached. ②.Added: 增加的状态. ③. ...

  2. Golang读取并修改非主流配置文件

    今天工作中碰到的问题,要求修改此配置文件,没看出来是什么格式,用了下面的思路: mysql { # If any of the files below are set, TLS encryption ...

  3. 来自后端的逆袭 blazor简介 全栈的福音

    背景 什么是SPA 什么是MPA MPA (Multi-page Application) 多页面应用指的就是最传统的 HTML 网页设计,早期的网站都是这样的设计,所之称为「网页设计」.使用 MPA ...

  4. JQuery EasyUI Tree组件的Bug记录

    记录一下使用项目中使用EasyUI遇到的bug,废话少说直接上菜  - _-(bug)..... bug ::   .netcore创建一个web应用时候,会自动引入jQuery库以及一些插件,但是在 ...

  5. Java自学-类和对象 属性初始化

    如何进行Java的属性初始化 步骤 1 : 对象属性初始化 对象属性初始化有3种 声明该属性的时候初始化 构造方法中初始化 初始化块 . public class Hero { public Stri ...

  6. Java自学-面向对象 属性

    Java类的属性 一个英雄有姓名,血量,护甲等等状态 这些状态就叫做一个类的属性 步骤 1 : 属性的类型 属性的类型可以是基本类型,比如int整数,float 浮点数 也可以是类类型,比如Strin ...

  7. Jenkins+Gitee异常解决

    Failed to connect to repository : Command "git ls-remote -h username@mygit.com:cc/myproject.git ...

  8. 9.Javascript原生瀑布流

      <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...

  9. jmeter-Unable to access jarfile ApacheJMeter.jar

    jmeter在运行时报错Unable to access jarfile ApacheJMeter.jar.如下图: 检查后发现jmeter_home/bin/目录下缺失 ApacheJMeter.j ...

  10. Java 之 数学相关类 Math、BigInteger、BigDecimal

    一.java.lang.Math 类 一.Math 类概述 java.lang.Math 类包含用于执行基本数学运算的方法,如指数.对数.平方根和三角函数.类似于这样的类,其所有方法均为静态方法,并且 ...