C - Dungeon Master

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

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. ####E
  14. 1 3 3
  15. S##
  16. #E#
  17. ###
  18. 0 0 0

Sample Output

  1. Escaped in 11 minute(s).
  2. Trapped!
  3.  
  4. //用dfs做的,就是在试探的时候多试探一次上下位置的时候就行。

  1. #include <iostream>
  2. #include <queue>
  3. #include <string.h>
  4. using namespace std;
  5. struct point
  6. {
  7. int x,y,h;
  8. int step;
  9. };
  10. point star,end;
  11. char map [][][];
  12. bool way [][][];
  13. int h,x,y;
  14. void read_map()//读地图
  15. {
  16. for (int i=;i<=h;i++)
  17. {
  18. for (int j=;j<=x;j++)
  19. {
  20. for (int k=;k<=y;k++)
  21. {
  22. cin>>map[i][j][k];
  23. if (map[i][j][k]=='S')
  24. {
  25. star.h=i;
  26. star.x=j;
  27. star.y=k;
  28. star.step=;
  29. }
  30. if (map[i][j][k]=='E')
  31. {
  32. end.h=i;
  33. end.x=j;
  34. end.y=k;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. int check(point t)//检查是否能走
  41. {
  42. if ( t.x>= && t.x<=x && t.y>= && t.y<=y && t.h>= && t.h<=h && way[t.h][t.x][t.y]== )
  43. {
  44. if ( map[t.h][t.x][t.y]!='#')
  45. {
  46. return ;
  47. }
  48. }
  49. return ;
  50. }
  51. int bfs()
  52. {
  53. point now,next;
  54. int min=-;
  55. queue<point> Q;
  56. Q.push(star);
  57. way[star.h][star.x][star.y]=;
  58. while (!Q.empty())
  59. {
  60. now=Q.front();
  61. Q.pop();
  62. if (now.x==end.x&&now.y==end.y&&now.h==end.h)
  63. {
  64. min=now.step;
  65. break;
  66. }
  67. next.x=now.x+;
  68. next.y=now.y;
  69. next.h=now.h;
  70. next.step=now.step+;
  71. if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=;}
  72. next.x=now.x;
  73. next.y=now.y-;
  74. next.h=now.h;
  75. next.step=now.step+;
  76. if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=;}
  77. next.x=now.x-;
  78. next.y=now.y;
  79. next.h=now.h;
  80. next.step=now.step+;
  81. if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=;}
  82. next.x=now.x;
  83. next.y=now.y+;
  84. next.h=now.h;
  85. next.step=now.step+;
  86. if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=;}
  87. next.x=now.x;
  88. next.y=now.y;
  89. next.h=now.h+;
  90. next.step=now.step+;
  91. if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=;}
  92. next.x=now.x;
  93. next.y=now.y;
  94. next.h=now.h-;
  95. next.step=now.step+;
  96. if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=;}
  97. }
  98. return min;
  99. }
  100. int main()
  101. {
  102. int all;
  103. while (cin>>h>>x>>y)
  104. {
  105. memset(way,,sizeof(way));
  106. if (h==&&x==&&y==) break;
  107. read_map();
  108. all=bfs();
  109. if (all==-)
  110. cout<<"Trapped!"<<endl;
  111. else
  112. cout<<"Escaped in "<<all<<" minute(s)."<<endl;
  113. }
  114. return ;
  115. }

 

C - Dungeon Master的更多相关文章

  1. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  2. poj 2251 Dungeon Master

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

  3. Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...

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

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

  5. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

  6. Dungeon Master poj 2251 dfs

    Language: Default Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16855 ...

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

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

  8. BFS POJ2251 Dungeon Master

    B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  9. POJ 2251 Dungeon Master (非三维bfs)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55224   Accepted: 20493 ...

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

随机推荐

  1. Win7如何改变用户文件夹位置

    现在装WIN7后第一件就是改变用户账户文件夹位置..因为里面存着一些软件的设定和信息等..不必要每次装后都手动改一次.. 已前用优化大师改.太麻烦.也不稳定有时有些目录不能完全改过来.. 通过命令mk ...

  2. 怎样实现广度优先遍历(BFS)

    BFS过程: 一:訪问顶点V,并标记V为已经訪问 二:顶点V入队列 三:假设队列非空.进行运行,否则算法结束 四:出队列取得对头顶点u,假设顶点未被訪问,就訪问该顶点,并标记该顶点为已经訪问 五:查找 ...

  3. Python Socket API 笔记

    将上节中的C#该成Python版的容易程度大大超出了我的意料之外.从来没有发现,仅仅用灰尘简单的几句话就实现了该程序的主要功能,可见python的简易和强大之处.这里先对SocketAPI 做一下总结 ...

  4. JavaScriptl 类数组转换为数组

    slice和Array.form方法,具体见示例代码: <!DOCTYPE html> <html lang="zh"> <head> < ...

  5. linux 终端分屏命令vsp(转)

    比如:某文件夹下有文件:vector.cc, substr.cc 1.使用vim命令打开任意一个文件:vim vector.cc打开第一个文件.如下图所示: 2.按:"Esc"键 ...

  6. CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡(转)

    一.简介 VS/NAT原理图: 二.系统环境 实验拓扑: 系统平台:CentOS 6.3 Kernel:2.6.32-279.el6.i686 LVS版本:ipvsadm-1.26 keepalive ...

  7. centos下nginx启动脚本和chkconfig管理

    在安装完nginx后,重新启动需要“kill -HUP nginx进程编号”来进行重新加载,显然十分不方便.如果能像apache一样,直接通过脚本进行管理就方便多了. nginx官方早就想好了,也提供 ...

  8. 从第三方Launcher授权启动指定APP的设计与实现

    Case 背景: Case要求从第三方Launcher中首次启动指定的应用程序时.弹出对话框提示用户进行授权启动,若用户未授权,则在下次再次启动该应用时依旧弹出对话框提示用户进行授权.直到用户相应用进 ...

  9. QT学习二:工具栏

    QT  的工具栏提供 Dock 功能.文字/图标显示排列的功能. void MainWindow::createToolBar(void) { QToolBar *toolBar = new QToo ...

  10. kafka consumer assign 和 subscribe模式差异分析

    转载请注明原创地址:http://www.cnblogs.com/dongxiao-yang/p/7200971.html 最近需要研究flink-connector-kafka的消费行为,发现fli ...