hdu1010Tempter of the Bone(迷宫dfs+剪枝)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 151147 Accepted Submission(s): 40285
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
题意:走迷宫,X表示墙,.表示通路,S起点,D终点。问是否能刚好在T秒的时候走到终点(每秒只能走一步,不能往回走),可以往上下左右四个方向走
题解:典型dfs,但要注意剪枝,看上去数据量不大,但不剪枝会T到怀疑人生。有两个地方可以剪枝,第一个是从当前点走到终点的最短距离(曼哈顿距离)比剩余时间大,那么就不可能到达终点了。第二个是剩余时间减去当前点到终点的最短距离是奇数的时候,不可能到达终点。
#include<bits/stdc++.h>
using namespace std;
int n,m,t;
char s[][];
int dirx[]= {,-,,};
int diry[]= {,,-,};
int sx,sy,ex,ey;
bool dfs(int x,int y,int time)//t表示剩余多少时间
{
if(time==)
{
if(x==ex&&y==ey)return true;
return false;
}
//剩余时间连理想最短路径都不够走
if(time<(abs(ex-x)+abs(ey-y))||(time-abs(ex-x)-abs(ey-y))%==)
return false;
for(int i=; i<; i++)
{
int xx=x+dirx[i];
int yy=y+diry[i];
if(xx<||yy<||xx>=n||yy>=m||s[xx][yy]=='X')continue;
s[xx][yy]='X';
if(dfs(xx,yy,time-))return true;
s[xx][yy]='.'; }
return false; }
int main()
{
while(~scanf("%d%d%d",&n,&m,&t),n+m+t)
{
memset(s,'\0',sizeof(s));
for(int i=; i<n; i++)
{
getchar();
for(int j=; j<m; j++)
{
scanf(" %c",&s[i][j]);
if(s[i][j]=='S')
{
sx=i;
sy=j;
s[i][j]='.';
}
if(s[i][j]=='D')
{
ex=i;
ey=j;
s[i][j]='.';
}
}
} s[sx][sy]='X';
if(dfs(sx,sy,t))printf("YES\n");
else printf("NO\n"); }
return ;
}
hdu1010Tempter of the Bone(迷宫dfs+剪枝)的更多相关文章
- 【HDU - 1010】Tempter of the Bone(dfs+剪枝)
Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱 ...
- HDOJ-1010 Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 给出一个n*m的迷宫 X为墙 .为空地 S为起点 D为终点 给出时间T 每走一步花费一单位的时间 走过的空 ...
- Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu1010--Tempter of the Bone(迷宫)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Jav ...
- Tempter of the Bone(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- HDU 1010 Tempter of the Bone (DFS+剪枝)
题意:从S走到D,能不能恰好用T时间. 析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey), 那么从(x, y)到 ...
- Hdu1010Tempter of the Bone 深搜+剪枝
题意:输入x,y,t.以及一个x行y列的地图,起点‘S’终点‘D’地板‘.’墙壁‘X’:判断能否从S正好走t步到D. 题解:dfs,奇偶性减枝,剩余步数剪枝. ps:帮室友Debug的题:打错了两个字 ...
- HDU1010 --- Tempter of the Bone(dfs+剪枝)
小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t ...
- hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...
随机推荐
- BZOJ 2120 数颜色 【带修改莫队】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 2120: 数颜色 Time Limit: 6 Sec Memory Limit: ...
- PHP单链表的基本操作
链表的实现 数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个X 上代 ...
- DU1525 Euclid's Game 博弈
HDU1525 Euclid's Game 博弈 题意 给定两个数字 a, b. 每次只能用 较大的值 减去 较小的值的倍数, 两个人轮流进行操作, 第一个得到 0 的胜利. 分析 对于 a == b ...
- UITableView控件Protocell的Identifier设置 注意事项
1. 注意:如果想使用Subtitle类型的单元格,需在Storyboard中将Protocell设置为subtitle类型,且Protocell的identifier必须与ViewControll ...
- 第25章 串行FLASH文件系统FatFs
25.1 文件系统 即使读者可能不了解文件系统,读者也一定对“文件”这个概念十分熟悉.数据在PC上是以文件的形式储存在磁盘中的,这些数据的形式一般为ASCII码或二进制形式.在上一章我们已经写好了Q ...
- Extjs header column 自定义排序规则
Extjs 的表格自带排序功能,这个功能在大部分情况下能够满足我们的需求,但是在某种情况下,例如IP排序,默认情况下,按照字符串进行排序, 此时我们需要自定义排序规则,这个时候就需要我们重写方法了, ...
- (Nagios)-check_openmanage[Dell]
Nagios->check_openmanage[Dell R7*] 2014年11月13日 下午 07:44 需求介绍: 透过Nagios监控Dell R7系列服务器硬件状态 环境信息: ...
- multiprocessing中进程池,线程池的使用
multiprocessing 多进程基本使用 示例代码1 import time import random from multiprocessing import Process def run( ...
- 「PHP」抽象工厂模式
引言 所属:创建型模式,常用设计模式之一 参考资料: <大话设计模式>程杰 模式概述 官方定义:抽象工厂模式(Abstract Factory),提供一个创建一系列相关或互相 ...
- day 28 黏包及黏包解决方案
1.缓冲区 每个socket被创建以后,都会分配两个缓冲区,输入缓冲区和输出缓冲区,默认大小都是8k,可以通过getsocket()获取,暂时存放传输数据,防止程序在发送的时候卡阻,提高代码运行效率. ...