hdu1010-Tempter of the Bone-(dfs+奇偶剪枝)
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
1 0 1 0
假设红色1要走到蓝色1,最短只需要走2步,但是现在有4步,必须绕远,绕过粉色1和粉色0走到蓝色1,如果是3步,怎么走都走不到。
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的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
随机推荐
- [转帖]AWR报告参数:DB TIME和DB CPU
AWR报告参数:DB TIME和DB CPU http://blog.itpub.net/12679300/viewspace-1182396/ 一.前言:AWR报告是了解ORACLE运行的一个重要报 ...
- Hystrix实现ThreadLocal上下文的传递 转
springcloud微服务中, 服务间传输全局类参数,如session信息等. 一.问题背景 Hystrix有2个隔离策略:THREAD以及SEMAPHORE,当隔离策略为 THREAD 时,是没办 ...
- Scala Collection Method
接收一元函数 map 转换元素,主要应用于不可变集合 (1 to 10).map(i => i * i) (1 to 10).flatMap(i => (1 to i).map(j =&g ...
- Grafana邮件报警
一.概述 报警是Grafana的一项革命性功能,它让Grafana从一个数据可视化工具变成一个真正的任务监控工具.报警规则可以使用现有的图表控制面板设置,阈值可以通过拖拉右边的线控制,非常简单.Gra ...
- count和distinct
一.count和distinct count是统计数据条数,distinct是去掉重复列: count统计的时候会忽略null值,distinct会将重复的null值列作为一个. 综上select c ...
- SQL Server SSIS中的变量使用表达式后,就无法更改其值了
在SQL Server SSIS中,我们可以为变量定义初始值和表达式,其实SSIS的变量定义为表达式后我们就无法更改变量的值了,我们来做如下实验: 首先我们在SSIS包中定义一个String类型的变量 ...
- MVC中使用SignalR打造酷炫实用的即时通讯功能(轉載)
資料來源:http://www.fangsi.net/1144.html 前言,现在这世道写篇帖子没个前言真不好意思发出来.本贴的主要内容来自于本人在之前项目中所开发的一个小功能,用于OA中的即时通讯 ...
- C#基础语法,快速上収C#
C#代码基础语法 对新手的帮助很大,可以尝试多看看然后在敲敲 // 单行注释以 // 开始 /* 多行注释是这样的 */ /// <summary> /// XML文档注释 /// < ...
- VS2017 注册
Visual Studio 2017 序列号 Key 激活码 VS2017 注册码 Visual Studio 2017(VS2017) 企业版 Enterprise 注册码 序列号:NJVYC-BM ...
- Object中defineProperty数据描述
Object.defineProperty是对对象中的属性进行数据描述的 使用语法: Object.defineProperty(obj,prop,descriptor) 使用示例: var data ...