Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27520   Accepted: 10776

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line

Trapped!

Sample Input

  1. 3 4 5
  2. S....
  3. .###.
  4. .##..
  5. ###.#
  6.  
  7. #####
  8. #####
  9. ##.##
  10. ##...
  11.  
  12. #####
  13. #####
  14. #.###
  15. ####E
  16.  
  17. 1 3 3
  18. S##
  19. #E#
  20. ###
  21.  
  22. 0 0 0

Sample Output

  1. Escaped in 11 minute(s).
  2. Trapped!
————————————分割线————————————
 
广搜

  1. #include "iostream"
  2. #include "cstdio"
  3. #include "fstream"
  4. #include "sstream"
  5. #include "cstring"
  6.  
  7. using namespace std;
  8. const int maxN = 3e4 + 1e2 ;
  9. struct Queue { int x , y , z ; } ;
  10. typedef long long QAQ ;
  11.  
  12. int dx[ ] = { , - , , , , } ;
  13. int dy[ ] = { , , , , - , } ;
  14. int dz[ ] = { , , - , , , } ;
  15.  
  16. int step[ maxN ] ;
  17. Queue Q[ maxN ] ;
  18. bool vis[ ][ ][ ] ;
  19. char map[ ][ ][ ] ;
  20.  
  21. int L , R , C ;
  22. int start_x , start_y , start_z , des_x , des_y , des_z ;
  23. //QAQ Ans ;
  24.  
  25. void Init ( ) {
  26. memset ( vis , false , sizeof ( vis ) ) ;
  27. memset ( step , , sizeof ( step ) ) ;
  28. }
  29.  
  30. inline bool Check ( const int x_x , const int y_y , const int z_z ) {return ( x_x == des_x && y_y == des_y && z_z == des_z ) ? true : false ; }
  31. int BFS ( ) {
  32. int Head = , Tail = ;
  33. Q[ Tail ].x = start_x ;
  34. Q[ Tail ].y = start_y ;
  35. Q[ Tail ].z = start_z ;
  36. while ( Head <= Tail ) {
  37. for ( int i= ; i< ; ++i ) {
  38. int xx = Q[ Head ].x + dx[ i ] ;
  39. int yy = Q[ Head ].y + dy[ i ] ;
  40. int zz = Q[ Head ].z + dz[ i ] ;
  41. if( !vis[ xx ][ yy ][ zz ] && ( map[ xx ][ yy ][ zz ] == '.' ) && xx >= && xx < L && yy >= && yy < R && zz >= && zz < C ) {
  42. vis[ xx ][ yy ][ zz ] = true ;
  43. Q[ ++Tail ].x = xx ;
  44. Q[ Tail ].y = yy ;
  45. Q[ Tail ].z = zz ;
  46. step [ Tail ] = step[ Head ] + ;
  47. if ( Check ( xx , yy , zz ) ) return step[ Tail ] ;
  48. }
  49. }
  50. ++ Head ;
  51. }
  52. return false ;
  53. }
  54.  
  55. void Scan ( ) {
  56. for ( int i= ; i<L ; ++i , getchar ( ) ){
  57. for ( int j= ; j<R ; ++j , getchar ( ) ){
  58. for ( int k= ; k<C ; ++k ) {
  59. map[ i ][ j ][ k ] = getchar ( ) ;
  60. if ( map[ i ][ j ][ k ] == 'S' ) {
  61. start_x = i ;
  62. start_y = j ;
  63. start_z = k ;
  64. }
  65. else if ( map[ i ][ j ][ k ] == 'E' ) {
  66. map[ i ][ j ][ k ] = '.' ;
  67. des_x = i ;
  68. des_y = j ;
  69. des_z = k ;
  70. }
  71. }
  72. }
  73. }
  74. }
  75.  
  76. inline void Print ( int temp ) {
  77. if( temp ) printf ( "Escaped in %d minute(s).\n" , temp ) ;
  78. else printf ( "Trapped!\n" ) ;
  79. }
  80.  
  81. int main ( ) {
  82. while ( scanf ( "%d %d %d\n " , &L , &R , &C ) == && L && R && C ) {
  83. Init ( ) ;
  84. Scan ( );
  85. Print ( BFS( ) ) ;
  86. }
  87. return ;
  88. }

2016-10-19 18:50:40

(完)

POJ 2251 题解的更多相关文章

  1. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  2. 【POJ 2251】Dungeon Master(bfs)

    BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...

  3. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

  4. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  5. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

  6. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  7. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  8. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

  9. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. jquery + header

    官网上搜索headers 基本用法(直接用下楼上的代码了) $.ajax({ //请求类型,这里为POST type: 'POST', //你要请求的api的URL url: url , //是否使用 ...

  2. mysql中find_in_set()函数的使用

    首先举个例子来说: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点.4图文等等 .现在有篇文章他既是头条,又是热点,还是图文,type中以 1,3,4 的格式存储.那我 ...

  3. [Nhibernate]对象状态

    目录 写在前面 文档与系列文章 对象状态 瞬时态(Transient) 持久态(Persistent) 托管态(Detached) 对象状态转换 总结 写在前面 前面两篇文章介绍了SchemaExpo ...

  4. 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

    错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...

  5. postgresql利用pg_upgrade升级数据库(从8.4升级到9.5)

    其他见:http://my.oschina.net/ensn/blog/636766 本文利用pg_upgrade实现将8.4.18版本升级到9.5.0版本,8.4.18版本为RedHat系统自带pg ...

  6. C#中Trim()、TrimStart()、TrimEnd()的用法

    string s = " from dual union all ";          s = s.Trim().TrimEnd("union all".To ...

  7. jq的$()里面 一定要是字符串类型!!!!!!!!!!!!!!!!!!!!!!!!

    var s = "[value="+uid+"]"; $(s).attr("checked",'true');

  8. PHP安装模式cgi、fastcgi、php_mod比较

    先了解一下普通cgi的工作流程: web server收到用户请求,并把请求提交给cgi程序,cgi程序根据请求提交的参数作相应处理,然后输出标准的html语句返回给web server,web se ...

  9. GitHub使用教程

    一直以来都想使用Git来管理自己平时积累的小代码,就是除了工作之外的代码了.有时候自己搞个小代码,在公司写了,就要通过U盘或者网盘等等一系列工具进行Copy,然后回家才能继续在原来的基础上作业.Cop ...

  10. php换行符

    1.需求 统一php换行符 2.实践 使用PHP_EOL替换换行符,保证平台的兼容性. 类似的有DIRECTORY_SEPARATOR 参考文档:http://www.cnblogs.com/code ...