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. 备份&添加无线网络配置

    netsh wlan export profile key=clear folder=c:\ #备份 (ls c:\*.xml).FullName|%{netsh wlan add profile f ...

  2. js 获取后台数据分页

    页面创建一个存放内容的容器,以及分页的容器: <div id="content"></div> <div id="pager"&g ...

  3. STS使用git下载项目代码

    在自己的eclipse 上安装git 插件,一般都自带了现在. 4.选择Clone URI 5.下一步输入刚才的复制的路劲,填写自己的github 账户名密码即可 6.选择要克隆的分支 7.设置本地g ...

  4. 840. Magic Squares In Grid (5月27日)

    开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...

  5. 浅析MySQL主从复制技术(异步复制、同步复制、半同步复制)

      Preface       As we all know,there're three kinds of replication in MySQL nowadays.Such as,asynchr ...

  6. WEB中需求分析应该考虑的问题

    一. 针对用户群体要考虑因素 1.用户年龄 2.选择素材 3.网站布局 4.颜色搭配 5. 用户体验及动效 6.功能便捷 用户需求.用户兴趣爱好.性格.职业.教育水平高低.消费观念.PC端和移动端哪一 ...

  7. Kafka 学习翻译 - 介绍

    Kafka是一个分布式的流式平台.可以从几个方面理解: 1. 三个重要的能力: 能够实现流式的发布和订阅数据,类似于消息队列或者企业级的消息分发系统. 能够在提供一定容错性和持久性能力的基础上存储数据 ...

  8. day 12 生成器和生成器函数以及各种推导式

    一.生成器    本质就是迭代器. 我们可以直接执⾏__next__()来执⾏ 以下⽣成器 一个一个的创建对象 创建生成器的方式: 1.生成器函数 2.通过生成器 表达式来获取生成器 3.类型转换(看 ...

  9. python的基本知识

    1. python的简介    python的创始⼈人为吉多·范罗苏姆(Guido van Rossum).1989年年的圣诞节期间,吉多· 范罗苏姆为了了在阿姆斯特丹丹打发时间,决⼼心开发⼀个新的脚 ...

  10. python中如何退出多层循环

    1.定义标记变量:利用变量值的变化退出循环 # 第一种嵌套形式 a = [[1, 2, 3], [5, 5, 6], [7, 8, 9]] # init_i = 0 # init_j = 0 flag ...