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

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

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.

 
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

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

 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
 
Sample Output
NO
YES

题意:走迷宫,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+剪枝)的更多相关文章

  1. 【HDU - 1010】Tempter of the Bone(dfs+剪枝)

    Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱 ...

  2. HDOJ-1010 Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 给出一个n*m的迷宫 X为墙 .为空地 S为起点 D为终点 给出时间T 每走一步花费一单位的时间 走过的空 ...

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

  4. hdu1010--Tempter of the Bone(迷宫)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Jav ...

  5. Tempter of the Bone(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  6. HDU 1010 Tempter of the Bone (DFS+剪枝)

    题意:从S走到D,能不能恰好用T时间. 析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey), 那么从(x, y)到 ...

  7. Hdu1010Tempter of the Bone 深搜+剪枝

    题意:输入x,y,t.以及一个x行y列的地图,起点‘S’终点‘D’地板‘.’墙壁‘X’:判断能否从S正好走t步到D. 题解:dfs,奇偶性减枝,剩余步数剪枝. ps:帮室友Debug的题:打错了两个字 ...

  8. HDU1010 --- Tempter of the Bone(dfs+剪枝)

    小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t ...

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

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

随机推荐

  1. 20145223 杨梦云 《网络对抗》 MSF基础应用

    20145223 杨梦云 <网路对抗> MSF基础应用 1.实验后回答问题:用自己的话解释什么是exploit,payload,encode (1)百度百科上说Exploit 的英文意思就 ...

  2. Jenkins+maven(testng)项目(本地项目配置)

    一.前提: 1. Jenkins下载安装. 2. testng的Maven项目. 二.检测: maven项目自身配置及检测. 1. POM.XML配置文件增加: <build> <p ...

  3. Spring 4 mvc+shiro+thymeleaf+JPA(Hibernate)+MySql eclipse项目模板

    本模板基本配制为:spring 4.3.8+thymeleaf 3.0.3 +hibernate 5.5.5 + mysql 5.7 IDE:eclipse 运行环境为:Tomcat 8.0.28 项 ...

  4. 十三、IntelliJ IDEA 中的版本控制介绍(下)

    我们已经简单了解了 IntelliJ IDEA 的版本控制机制,那么接下来,就让我们一起看看在 IntelliJ IDEA 中进行具体的版本控制操作. 标注1:Checkout from Versio ...

  5. java 中重载(Overload)和重写(Override)的区别

    首先重载和重写是应用于两个不同场景下面的两种不同的手段: 两者各自的特征: 重载(Overload):首先是位于一个类之中或者其子类中,具有相同的方法名,但是方法的参数不同,返回值类型可以相同也可以不 ...

  6. TCP-IP and Advanced Topics 课程总结与报告

    课程总结 学习了四周十六课的课程,对每一课的知识点进行总结梳理,作出一个树状的知识网络图. 本课程虽然在深度上有所欠缺,但却更有利于结构上的梳理,加深总体上对网络的理解. 本课程从Internet出发 ...

  7. C# 5.0中使用CallerMemberName、CallerFilePath和CallerLineNumber获取代码的调用方信息(转载)

    很多时候,我们需要在运行过程中记录一些调测的日志信息,如下所示: public void DoProcessing() { TraceMessage("DoProcessing()被XXX调 ...

  8. jquery实现页面图片轮播

    1.创建一个html页面 <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...

  9. Oracle中常用的语句

    1.查询锁表 SELECT a.object_name,b.session_id,c.serial#,c.program,c.username,c.command,c.machine,c.lockwa ...

  10. ORACLE学习之三

    DDL 数据定义语言 CREATE ALTER DROP DML 数据操作语言 INSERT UPDATE DELETE DQL 数据查询语言 SELECT TCL 事务控制语言 COMMIT ROL ...