Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 107138    Accepted Submission(s): 29131

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

题目大意是要在那个时间点找到D点,典型的DFS问题,重点是要求奇偶减枝

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <cmath>
  4. #include <iostream>
  5. using namespace std;
  6. #define Maxn 100
  7.  
  8. int hang,lie,Time;
  9. char MAP[Maxn][Maxn];
  10. int begin_x,begin_y;
  11. int end_x,end_y;
  12. bool flag;
  13. int dir[4][2] = {
  14. {0,1},
  15. {0,-1},
  16. {1,0},
  17. {-1,0}
  18. };
  19.  
  20. void print()
  21. {
  22. for(int i = 0; i < hang; i++)
  23. {
  24. for(int j = 0; j < lie; j++)
  25. {
  26. printf("%c",MAP[i][j]);
  27. }
  28. printf("\n");
  29. }
  30. }
  31.  
  32. void dfs(int x, int y, int t)
  33. {
  34. // print();
  35. // printf("\n");
  36. if (flag)
  37. {
  38. return ;
  39. }
  40. // printf("Q\n", );
  41. if (x == end_x && y == end_y && t == Time)
  42. {
  43. // printf("YES~~~~~~~~~~~\n");
  44. flag = true;
  45. return ;
  46. }
  47. int temp = (Time - t) - ( abs(x- end_x) + abs(y - end_y) );
  48. if (temp < 0 || temp & 1)
  49. {
  50. return ; // 奇偶剪枝
  51. /*要理解奇偶剪枝,先了解一下曼哈顿距离,
  52. 从一个点到达另外一个点的最
  53. 短路径长度(时间)可以根据两点坐标求出,
  54. 路径长度(非最短)与最短路径的长度同奇偶,
  55. 它们的差一定是偶数!举个例子,就像两个偶数的差
  56. 差是偶数,两个个数的差也是偶数.*/
  57. }
  58. for(int i = 0; i < 4; i++)
  59. {
  60. int xx = x + dir[i][0];
  61. int yy = y + dir[i][1];
  62. if (xx >= 0 && xx < hang && yy >= 0 && yy < lie)
  63. {
  64. if (MAP[xx][yy] != 'X')
  65. {
  66. MAP[xx][yy] = 'X';
  67. dfs(xx,yy,t+1);
  68. MAP[xx][yy] = '.';
  69. }
  70. }
  71. }
  72. return ;
  73. }
  74.  
  75. int main()
  76. {
  77. while(cin >> hang >> lie >> Time,hang + lie + Time)
  78. {
  79. flag = false;
  80. for(int i = 0; i < hang; i++)
  81. {
  82. scanf("%s",MAP[i]);
  83. }
  84. for(int i = 0; i < hang; i++)
  85. {
  86. for(int j = 0; j < lie; j++)
  87. {
  88. if (MAP[i][j] == 'S')
  89. {
  90. begin_x = i;
  91. begin_y = j;
  92. }
  93. else if (MAP[i][j] == 'D')
  94. {
  95. end_x = i;
  96. end_y = j;
  97. }
  98. }
  99. }
  100. // printf("%d %d\n",begin_x,begin_y);
  101. MAP[begin_x][begin_y] = 'X';
  102. dfs(begin_x,begin_y,0);
  103. if (flag)
  104. {
  105. printf("YES\n");
  106. }
  107. else
  108. {
  109. printf("NO\n");
  110. }
  111. }
  112. }

  

hdu 1010 dfs搜索的更多相关文章

  1. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  2. hdu 1010(DFS) 骨头的诱惑

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意从S出发,问能否在时间t的时候到达终点D,X为障碍 需要注意的是要恰好在t时刻到达,而不是在t时间 ...

  3. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  4. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  5. Tempter of the Bone HDU 1010(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

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  7. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  8. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. Tempter of the Bone HDU - 1010(dfs)

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

随机推荐

  1. 关于for循环中的闭包问题

    还是昨天的那个简单的小项目,已经花了一天的时间了 - - .从&&的用法,到CSStext,到今天马上要谈的闭包(closure),通过一个小东西,真真发现了自己的各方面不足.昨天发完 ...

  2. java web工程的错误页面的简单配置

    jsp页面,本身服务器也会将该页面翻译成一个servlet页面,所以请求该页面就会有可能出现错误的情况,就会出现下面类似的页面 这样给客户看到并不友好. 1.jsp页面<%@ page %> ...

  3. Memento 模式

    Memento 模式的关键就是要在不破坏封装行的前提下,捕获并保存一个类的内部状态,这样就可以利用该保存的状态实施恢复操作. /////////Originator.h//////////////// ...

  4. Sizzle引擎执行的流程图

    Sizzle有太多都不是太懂,但能看懂这张图. 图片来源: http://www.cnblogs.com/aaronjs/p/3332805.html

  5. 从外国html5网站上扒来一个鼠标经过的css3 效果,感觉很不错

    鼠标经过的时候,感觉有点像一张纸卷上去的感觉. 下面是代码 <div class="main-container types"> <div class=" ...

  6. Java之网络请求工具类(依赖:org.apache.http;注:HttpClient 4.4,HttpCore 4.4)

    到此处可以去下载依赖包:http://hc.apache.org/downloads.cgi import java.util.List; import org.apache.http.HttpSta ...

  7. 10个必看的PHP小代码,很实用!

    获取浏览器IP地址 function getRemoteIPAddress() { $ip = $_SERVER['REMOTE_ADDR']; return $ip; } 如果有代理服务器的情况下获 ...

  8. 将VIM配置成强大的IDE(三)

    上一节,我们知道了,我们了解了怎么配置插件的下下载. 现在,我们就可以去DIY我们的IDE了,主要介绍taglist插件和NERDTree插件,最终的结果是: 1.安装Taglist插件. Tagli ...

  9. java 上下文切换

    上下文概念 在高性能编程时,经常接触到多线程. 起初我们的理解是, 多个线程并行地执行总比单个线程要快, 就像多个人一起干活总比一个人干要快. 然而实际情况是, 多线程之间需要竞争IO设备, 或者竞争 ...

  10. [Quote]Creating basic Excel workbook with Open XML

    Creating basic Excel workbook with Open XML [Quote from]http://www.codeproject.com/Articles/371203/C ...