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的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
随机推荐
- 【C++】一个指针占几个字节?为什么呢?
一个指针在32位操作系统上,占4个字节 一个指针在64位操作系统上,占8个字节 但是,编译器为了兼容32位操作系统和64位操作系统,所以指针都是4个字节长度 为什么呢? 在计算机中,CPU不能直接与硬 ...
- TP5配置隐藏入口index.php文件
隐藏的index.php PS:这里说的入口文件指的是公共/ index.php文件,配置文件就在这个目录下 可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则 ...
- ASp.net Core EF ActionFilterAttribute AOP
在项目中经常遇到一些数据的修改,很多时候业务方需要一个修改日志记录,这里我们计划用mssql数据库来存放日志记录,用EF来操作,记录日志可以用mvc的ActionFilterAttribute 来完成 ...
- Django 定义视图函数
Django 定义视图函数 一.接收内容及文件处理 1.接收分类 # 获取数据 request.GET # 提交数据 request.POST # 获取文件 request.FILES 2.check ...
- django开发_七牛云图片管理
七牛云注册 https://www.qiniu.com/ 实名认证成功之后,赠送10G存储空间 复制粘贴AK和SK 创建存储空间,填写空间名称,选择存储区域.访问控制选择位公开空间 获取测试域名 七牛 ...
- NOIP2018 填数游戏 搜索、DP
LOJ 感觉这个题十分好玩于是诈尸更博.一年之前的做题心得只有这道题还记得清楚-- 设输入为\(n,m\)时的答案为\(f(n,m)\),首先\(f(n,m)=f(m,n)\)所以接下来默认\(n \ ...
- 第四周(1):数据分布-Python实战
数据准备 数据集地址:http://jse.amstat.org/datasets/normtemp.dat.txt 数据集描述:总共只有三列:体温.性别.心率 数据集详细描述:Journal of ...
- 任意图像尺寸变成目标尺寸(包含相应的boxes的变换)
def image_preporcess(image, target_size, gt_boxes=None): image = cv2.cvtColor(image, cv2.COLOR_BGR2R ...
- 2019-07-30 ThinkPHP文件上传
文件上传就是获取到待上传文件的临时路径,把它移动到服务器下的相应文件夹中. 文件上传,必须在表单中的form标签中写入:enctype="multipart/form-data" ...
- 解决javaScript在不同时区new Date()显示值不同问题
在日期格式化时遇到的问题,日期格式化方法在最下面 如果在中国时区 formatDate('2019-07-09') 结果是 ‘2019-07-09’ 如果 在夏威夷时区 utc-10:00 或 ...