MiYu原创, 转帖请注明 : 转载自 ______________白白の屋

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1010

题目描述:

代码

Tempter of the Bone

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

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

题目分析 :

传说中的 搜索题中具有里程碑意义的题目........

很好,很强大. 做过过这题大概也就明白了 搜索中的 奇偶剪枝以及路径. 加了剪枝的直接结果就是 TLE 和 46MS AC.......

刚开始做的时候,也没明白剪枝到底有什么作用, 所以直接敲了个DFS代码就交了, 答案很明显....TLE.  然后就去学习PPT去了.

所谓奇偶剪枝 :

  1. 把矩阵标记成如下形式:
  2. 0,1,0,1,0
  3. 1,0,1,0,1
  4. 0,1,0,1,0
  5. 1,0,1,0,1
  6. 很明显,如果起点在0 而终点在1 那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
  7. 在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了

所谓路径剪枝:

矩阵的大小是N*M 墙的数量记为wall 如果能走的路的数量 N*M - wall 小于时间T,就是说走完也不能到总的时间的,这显然是错误的,可以直接跳出了.

另外还有就是, 当DFS深度 > T 时,显然也不用继续找下去了. 那狗已经挂了.

所以在经过这3次剪枝后, 时间就大大缩短了.

值得一提的是!!! 这题我WA了很久, 一直找不原因,  因为数据不一定是按严格矩阵排列的!! 也可能都在一行!!!! 所以 无论是 gets 还是 getchar 都错的很冤枉.

使用 cin  和 scanf ("%s") 后 AC 了, 在此感谢 AMB神牛的帮忙.......

代码如下:

/*

MiYu原创, 转帖请注明 : 转载自 ______________白白の屋

Author By : MiYu

Test      : 1

Program   : HDU1010

*/

#include <iostream>

#include <ctime>

using namespace std;

typedef struct pos{

int x,y;

void setPos( int a = 0,int b = 0 ){ x = a; y = b; }

}POS;

POS start,end;

const int START = 10;  //看了就知道啥意思

const int DOOR = 20;

const int WALL = 0;

const int ROAR = 1;

int N = 0,M = 0,T = 0; //输入的

int maze[11][11];      //很明显是迷宫地图

int d[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };

int f = 1,roarCount = 0;  //标记是否找到路 , 记录可以走的路的个数

int DFS ( int x,int y,int n )

{

if ( n == T )     //时间用完了, 走到出口没 ?

{

if ( x == end.x && y == end.y )

f = 0;

return 0;

}

if ( f == 0 )     //已经找到路了, 不用找了

return 0;

int t = T - n - abs( x-end.x ) - abs( y-end.y );

if ( t < 0 || t % 2 == 1 )     //不够时间了 或 不可能走到(奇偶剪枝)

return 0;

for ( int i = 0; i != 4; ++ i )

{

if ( maze[ x+d[i][0] ][ y+d[i][1] ] != WALL )    //先看看下一步能不能走

{

maze[x+d[i][0]][y+d[i][1]] = WALL;    //走过后就不能走了

DFS ( x+d[i][0], y+d[i][1], n + 1 );  //走到下一个位置

if ( f == 0 )     //已经找到路了, 不用找了

return 0;

maze[x+d[i][0]][y+d[i][1]] = ROAR;    //没找到路,回溯下

}

}

return 0;

}

void init()  //在主函数一堆, 难看, 放外面了, 不解释

{

memset ( maze, 0, sizeof ( maze ) );

f = 1, roarCount = 0;

for ( int i = 1; i <= N; ++ i )

{

char ch;

for ( int j = 1; j <= M ; ++ j )

{

cin >> ch;

switch ( ch )

{

case 'S':  maze[i][j] = START; start.setPos ( i,j ); break;

case '.':  maze[i][j] = ROAR;  roarCount ++; break;

case 'X':  maze[i][j] = WALL;  break;

case 'D':  maze[i][j] = DOOR;  end.setPos ( i,j ); roarCount ++; break;

}

}

}

}

int main ()

{

while ( cin >> N >> M >> T, N + M + T )

{

init ();

if ( roarCount >= T )      //当然要保证能走的路比开门的时间要多

{

maze[start.x][start.y] = WALL;

DFS( start.x, start.y, 0 );

}

puts ( f == 1 ? "NO" : "YES" );

}

return 0;

}

另外附上一网友详细解释:

  1. 1010 temp of the bone
  2. sample input:
  3. 4 4 5
  4. S.X.
  5. ..X.
  6. ..XD
  7. ....
  8. 问题:
  9. (1):
  10. 在发现当前节点无法到达时,这点弹出栈,并且把这点的标记重新刷为'.'
  11. (2):
  12. 如何在dfs中既要保证到达又要使时间正好呢?? 在函数中通过这种形式实现:
  13. dfs(int si,int sj,int cnt) 就是用cnt来记录当时的时间,并且在
  14. if( si==di && sj==dj && cnt==t )
  15. {
  16. escape = 1;
  17. return;
  18. }
  19. 的时候 即当前点到达了终点并且时间恰好等于题目所给限制时间时,跳出
  20. 并且escape标记为真
  21. (3):
  22. 如何让一个点有顺序地遍历它四周地能到达的点呢??
  23. 聪明并且简短的方法是设施一个dir[4][2] 数组 控制方向
  24. 并且设置它的值为dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
  25. 遍历的时候用for(i:0->4)就非常方便了
  26. (4):
  27. 千万要注意的是节点越界的情况, dfs(int si,int sj,int cnt)的时候一定要把 si, sj 控制在给你的矩阵内 在后面会提到一个我的列子 就是因为访问了[0, -1]的位置导致了其
  28. 他数据被更改
  29. (5):
  30. 读入矩阵的时候,可以采用for(i = 1; i <= N; i++)
  31. for(j = 1; j <= M; j++)
  32. scanf("%c", &map[i][j]);
  33. 的方法,好处在于可以控制和计算每一个读入的数据,坏处是调试的时候对矩阵的观察不太方便,而且好像还会有错误,在2102"A计划"用这种方法读入数据时好像就会wa,
  34. 另一种方法是for(i = 0; i < N; i++) gets(map[i]);
  35. 这样读入的数据在调试观察的时候十分方便 gets()读入的默认为字符串,在vc调试的时候是显式的 可以直接观察矩阵 缺点是对矩阵中各个数据的计算和控制无法实现,需要读完后再遍历一遍
  36. (6)
  37. 能用bfs还是尽量用bfs 我不会bfs.... dfs的递归在调试的时候不是很方便,而且bfs要比dfs快,调试也要方便,因为它没有递归
  38. (7)
  39. 关于剪枝,没有剪枝的搜索不太可能,这题老刘上课的时候讲过两个剪枝,一个是奇偶剪枝,一个是路径剪枝
  40. 奇偶剪枝:
  41. 把矩阵标记成如下形式:
  42. 0,1,0,1,0
  43. 1,0,1,0,1
  44. 0,1,0,1,0
  45. 1,0,1,0,1
  46. 很明显,如果起点在0 而终点在1 那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
  47. 在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了
  48. 路径剪枝:
  49. 矩阵的大小是N*M 墙的数量记为wall 如果能走的路的数量 N*M - wall 小于时间T,就是说走完也不能到总的时间的,这显然是错误的,可以直接跳出了
  50. 课件里面给过这题的标程,在dfs的过程中有个没提到的剪枝,就是记录当前点到终点的最短路,如果小于剩余的时间的话,就跳出
  51. 这个剪枝我觉得更科学,它毕竟是动态的么,标程里面是这么写的:
  52. temp = (t-cnt) - abs(si-di) - abs(sj-dj);
  53. if( temp<0 || temp&1 ) return;
  54. 其中求当前点到终点的最短路是这样 abs(si-di) - abs(sj-dj) 这个就比较粗糙了 明显没有考虑到碰到墙要拐弯的情况
  55. 那求最短路有没有什么好办法呢?
  56. 我曾经想到过用 Dijkstraq求最短路的 ,明显大才小用,在论坛里看到一个方法觉得可以用在这里
  57. 给定下例:
  58. S.X.
  59. ..X.
  60. ..XD
  61. ....
  62. 每个点到终点的最短路是不是这样呢:
  63. S6X2
  64. 65X1
  65. 54XD
  66. 4321
  67. 这怎么求呢??从终点开始遍历整个数组,终点是0,它周围的点都+1,墙就不计数,依次类推,就能求得这个矩阵的一个最短时间矩阵,在dfs的时候比较当前点到终点的最短路,如果大于剩余时间的话就跳出
  68. 这个方法的预处理还是非常快的,我没有用过,但是感觉会非常有用处.
  69. (8)
  70. 在做这题的时候,我碰到过一个神奇的事情,在程序运行至下面代码时
  71. if( map[ si+dir[i][0] ][ sj+dir[i][1] ] != 'X')
  72. map[ si+dir[i][0] ][ sj+dir[i][1] ] = 'X';
  73. T被改变了!! 这丝毫和T没有关系啊,怎么改变T的值呢??
  74. 原来在起点map[0][0]进入时,我没有注意到map[ si+dir[i][0] ][ sj+dir[i][1] ] 实际做的是map[0][-1] = 'X'; 很危险的一个赋值,书本上千万次强调的东西让我碰上了,这个地方我找了很久,因此我觉得有必要单独列出来提醒自己
  75. //
  76. 下面我把一个带注释的标程贴一下,不是我写的注释
  77. //zju 2110 Tempter of the Bone
  78. #include <stdio.h>
  79. #include <iostream>
  80. #include <string.h>
  81. #include <stdlib.h>
  82. using namespace std;
  83. //迷宫地图
  84. //X: 墙壁,小狗不能进入
  85. //S: 小狗所处的位置
  86. //D: 迷宫的门
  87. //. : 空的方格
  88. char map[9][9];
  89. int n,m,t,di,dj; //(di,dj):门的位置
  90. bool escape;
  91. int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; //分别表示下、上、左、右四个方向
  92. void dfs(int si,int sj,int cnt)  //表示起始位置为(si,sj),要求在第cnt秒达到门的位置
  93. {
  94. int i,temp;
  95. if( si>n || sj>m || si<=0 || sj<=0 ) return;
  96. if( si==di && sj==dj && cnt==t )
  97. {
  98. escape = 1;
  99. return;
  100. }
  101. //abs(x-ex) + abs(y - ey)表示现在所在的格子到目标格子的距离(不能走对角线)
  102. //t-cnt是实际还需要的步数,将他们做差
  103. //如果temp < 0或者temp为奇数,那就不可能到达!
  104. temp = (t-cnt) - abs(si-di) - abs(sj-dj);
  105. if( temp<0 || temp&1 ) return;
  106. for( i=0; i<4; i++ )
  107. {
  108. if( map[ si+dir[i][0] ][ sj+dir[i][1] ] != 'X')
  109. {
  110. map[ si+dir[i][0] ][ sj+dir[i][1] ] = 'X';
  111. dfs(si+dir[i][0], sj+dir[i][1], cnt+1);
  112. if(escape) return;
  113. map[ si+dir[i][0] ][ sj+dir[i][1] ] = '.';
  114. }
  115. }
  116. return;
  117. }
  118. int main()
  119. {
  120. int i,j,si,sj;
  121. while( cin >> n >> m >> t)
  122. {
  123. if( n==0 && m==0 && t==0 )
  124. break;
  125. int wall = 0;
  126. for( i=1; i<=n; i++ )
  127. for( j=1; j<=m; j++ )
  128. {
  129. cin >> map[i][j];
  130. if(map[i][j]=='S') { si=i; sj=j; }
  131. else if( map[i][j]=='D' ) { di=i; dj=j; }
  132. else if( map[i][j]=='X' ) wall++;
  133. }
  134. if( n*m-wall <= t )
  135. {
  136. cout << "NO" << endl;
  137. continue;
  138. }
  139. escape = 0;
  140. map[si][sj] = 'X';
  141. dfs( si, sj, 0 );
  142. if( escape ) cout << "YES" << endl;
  143. else cout << "NO" << endl;
  144. }
  145. return 0;
  146. }

转载于:https://www.cnblogs.com/MiYu/archive/2010/08/18/1802753.html

HDOJ 1010 HDU 1010 Tempter of the Bone ACM 1010 IN HDU相关推荐

  1. HDU 2128 Tempter of the Bone II BFS

    状压整张图包括每个点的炸弹有没有被拿,墙壁有没有被炸.用优先队列存一下状态. 还有就是注意整数数溢出的问题. #include <cstdio> #include <cstring& ...

  2. HDOJ 2227 HDU 2227 Find the nondecreasing subsequences ACM 2227 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋   题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2227 题目描述: ...

  3. HDOJ 2066 HDU 2066 一个人的旅行 ACM 2066 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址:          http://acm.hdu.edu.cn/showproblem.php?pid=2066 ...

  4. HDU.1010 Tempter of the Bone

    文章目录 一.题目解读 1.原题 2.分类 3.题意 4.输入输出格式 5.数据范围 二.题解参考 1.总体思路 2.思路① (1).分析 (2).AC代码 三.评价与后话 1.评价 2.奇偶剪枝0- ...

  5. 回溯法+奇偶剪枝——Hdu 1010 Tempter of the Bone

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

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

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

  7. hdu1010 Tempter of the Bone

    转载自:http://acm.hdu.edu.cn/forum/read.php?tid=6158 sample input: 4 4 5 S.X. ..X. ..XD .... 问题: (1): 在 ...

  8. Tempter of the Bone(DFS + 奇偶剪枝,好题)

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

  9. HDU1010 Tempter of the Bone DFS+剪枝

    点击打开链接 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

最新文章

  1. linux之shell脚本学习篇一
  2. Spring中使用缓存时你应该知道的知识
  3. 【考证】华为HCIP、HCIE认证考试大纲
  4. springboot----shiro集成
  5. JavaScript:改变li前缀图片和样式
  6. redis源码剖析(十四)—— dump.rdb文件分析工具
  7. 23 岁创业,28 岁成为福布斯亚洲青年领袖,这个“刷脸的男人”有点牛
  8. 移动平台前端开发总结(针对iphone,Android等手机)
  9. jni hook java_java通过jni调用hook无效
  10. android studio for android learning (十七) Toast使用详解
  11. 用计算机上发微博,电脑版新浪微博怎么使用?新浪微博基本使用方法介绍
  12. DOSBox的安装及使用
  13. 关于E-R(实体-联系)图
  14. egret白鹭 基于eui组件的一些动画类 抽屉效果
  15. 02.图像分类任务介绍线性分类器(上).1080P
  16. 基于GPT硬盘模式重装win10操作系统
  17. 《正在爆发的互联网革命》北京西单图书大厦签售活动圆满结束
  18. error MSB6006: “CL.exe”已退出,代码为 2 问题
  19. 如何回答「为什么要雇用你」【面试核心问题4】
  20. XENAPP 7.6 和XENDESKTOP 7.6 初体验之一 安装

热门文章

  1. BZOJ 1086 [SCOI2005]王室联邦(树分块)
  2. 编译原理预测分析程序
  3. 李超线段树 [Heoi2013]Segment
  4. 转载:python引用DLL文件的方法
  5. 软件工程概论个人作业02(四则运算2)
  6. [转]unity3D游戏开发之GUI
  7. C#获取txt记事本内容,防止乱码情况
  8. HDOJ1014 Uniform Generator
  9. 对PostgreSQL中后台进程内存挂载的初步学习
  10. java将字符串转换成可执行代码