Tempter of the Bone

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 

题目大意:

一扇门只能在第T秒时打开,问小狗是否能在开门时恰好到达这扇门,逃出去。

解题思路:

DFS问题。

奇偶剪枝:
是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点, 
s        
|        
|        
|        
+ e
 
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步
数, 记做step,此处step1=8;
  
s  
  +  
| +      
|        
+ e
 
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);
故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
反之亦反。
 
代码:
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #define N 10
  6. using namespace std;
  7. bool flag,ans,visited[N][N];
  8. int n,m,t,xe,ye;
  9. char map0[N][N];
  10. void dfs(int x,int y,int timen){
  11. if(flag) return ;
  12. if(timen>t) return ;
  13. if(x<0||x>n-1||y<0||y>m-1) return ;
  14. if(timen==t&&map0[x][y]=='D') {flag=ans=true;return ;}
  15. int temp=t-timen-abs(xe-x)-abs(ye-y);
  16. if(temp&1) return ;//奇偶剪枝,位运算判断是否为奇数,比mod更快.
  17. if(!visited[x-1][y]&&map0[x-1][y]!='X'){
  18. visited[x-1][y]=true;
  19. dfs(x-1,y,timen+1);
  20. visited[x-1][y]=false;
  21. }
  22. if(!visited[x+1][y]&&map0[x+1][y]!='X'){
  23. visited[x+1][y]=true;
  24. dfs(x+1,y,timen+1);
  25. visited[x+1][y]=false;
  26. }
  27. if(!visited[x][y-1]&&map0[x][y-1]!='X'){
  28. visited[x][y-1]=true;
  29. dfs(x,y-1,timen+1);
  30. visited[x][y-1]=false;
  31. }
  32. if(!visited[x][y+1]&&map0[x][y+1]!='X'){
  33. visited[x][y+1]=true;
  34. dfs(x,y+1,timen+1);
  35. visited[x][y+1]=false;
  36. }
  37. }
  38. int main(){
  39. int xs,ys;
  40. while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(n||m||t)){
  41. int cnt=0;
  42. getchar();
  43. memset(visited,false,sizeof(visited));
  44. flag=ans=false;
  45. for(int i=0;i<n;i++){
  46. for(int j=0;j<m;j++){
  47. cin>>map0[i][j];
  48. if(map0[i][j]=='S'){
  49. xs=i;ys=j;
  50. visited[i][j]=true;
  51. }
  52. if(map0[i][j]=='D'){
  53. xe=i;ye=j;
  54. }
  55. if(map0[i][j]=='X'){
  56. cnt++;
  57. }
  58. }
  59. }
  60. if(n*m-cnt-1>=t) dfs(xs,ys,0);
  61. if(ans) printf("YES\n");
  62. else printf("NO\n");
  63. }
  64. return 0;
  65. }

HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】的更多相关文章

  1. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

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

  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(深搜+剪枝问题)

    Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...

  5. HDU1010 Tempter of the Bone(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

  6. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

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

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

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

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. java stoi

    package string.string1_4; import java.util.Scanner; public class StrToInt { /** * 将str转换为int整数 * 1. ...

  2. TXT文本写入数据库

    load data local infile "D:/abc.txt" into table lee; leedabao.txt内容如下,中间用Tab隔开: 2 yuanpeng ...

  3. 在VS2015中增加JQuery引用及智能提示

      打开VS2015,从"工具"菜单选择NuGet选项,搜索Jquery,并点击安装.   可以看到解决方案的scripts增加了对应文件的引用   在HTML文件中可以直接引用j ...

  4. JavaScript全讲-必知的特性

    上篇讲完JS面向对象的特性,我们今天就来聊聊它的其它特性. JS面向对象的特性较为隐晦,真正使用也是比較少的.而今天我们要讲的,却是在实际开发中常常遇到的特性. 1. 函数式编程 多数人并不知道&qu ...

  5. HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

  6. Oracle 数据库中序列结合触发器实现主键自增长

    一.数据表名称为T_OFFICE,其主键为PID(number类型) 二.首先为数据表的PID字段创建序列 序列名称:S_T_OFFICE_PID 序列详细内容: 三.创建相应的触发器 触发器名称:T ...

  7. Dropout: A Simple Way to Prevent Neural Networks fromOverfitting

    https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf Deep neural nets with a large number of par ...

  8. 理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux?

    作者:Wang Namelos链接:https://www.zhihu.com/question/41312576/answer/90782136来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...

  9. Redis缓存服务搭建及实现数据读写 - Eric.Chen

    发现博客园中好多大牛在介绍自己的开源项目是很少用到缓存,比如Memcached.Redis.mongodb等,今天得空抽时间把Redis缓存研究了一下,写下来总结一下,跟大家一起分享 一下.由于小弟水 ...

  10. 重学DSP:对于卷积的理解

    最近,我发现自己对于一个事情,如果不给自己一个说服自己的理由,就会出现不能理解,不能记住,以至于不会使用或者“盲目”应用的情况. 但是,我学的这个学科就是应当建立在对信号作用过程的理解上面的. 下面, ...