题目链接:http://poj.org/problem?id=2251

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37296   Accepted: 14266

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!

Source

题解:

三维的纯BFS。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. #define ms(a,b) memset((a),(b),sizeof((a)))
  13. using namespace std;
  14. typedef long long LL;
  15. const int INF = 2e9;
  16. const LL LNF = 9e18;
  17. const int MOD = 1e9+;
  18. const int MAXN = +;
  19.  
  20. struct node
  21. {
  22. int x, y, z, step;
  23. };
  24.  
  25. int L, R, C;
  26. char m[MAXN][MAXN][MAXN];
  27. int vis[MAXN][MAXN][MAXN];
  28. int dir[][] = { {,,},{,,},{,,},{-,,},{,-,},{,,-} };
  29.  
  30. queue<node>que;
  31. int bfs(node s, node e)
  32. {
  33. ms(vis,);
  34. while(!que.empty()) que.pop();
  35.  
  36. s.step = ;
  37. vis[s.x][s.y][s.z] = ;
  38. que.push(s);
  39.  
  40. while(!que.empty())
  41. {
  42. node now = que.front();
  43. que.pop();
  44.  
  45. if(now.x==e.x && now.y==e.y && now.z==e.z)
  46. return now.step;
  47.  
  48. for(int i = ; i<; i++)
  49. {
  50. node tmp;
  51. tmp.x = now.x + dir[i][];
  52. tmp.y = now.y + dir[i][];
  53. tmp.z = now.z + dir[i][];
  54. if(tmp.x>= && tmp.x<=L && tmp.y>= && tmp.y<=R && tmp.z>= && tmp.z<=C
  55. && m[tmp.x][tmp.y][tmp.z]!='#' && !vis[tmp.x][tmp.y][tmp.z] )
  56. {
  57. vis[tmp.x][tmp.y][tmp.z] = ;
  58. tmp.step = now.step + ;
  59. que.push(tmp);
  60. }
  61. }
  62. }
  63. return -;
  64. }
  65.  
  66. int main()
  67. {
  68. while(scanf("%d%d%d",&L, &R, &C) && (L||R||C))
  69. {
  70. for(int i = ; i<=L; i++)
  71. for(int j = ; j<=R; j++)
  72. scanf("%s", m[i][j]+);
  73.  
  74. node s, e;
  75. for(int i = ; i<=L; i++)
  76. for(int j = ; j<=R; j++)
  77. for(int k = ; k<=C; k++)
  78. {
  79. if(m[i][j][k]=='S') s.x = i, s.y = j, s.z = k;
  80. if(m[i][j][k]=='E') e.x = i, e.y = j, e.z = k;
  81. }
  82.  
  83. int ans = bfs(s,e);
  84. if(ans==-)
  85. printf("Trapped!\n");
  86. else
  87. printf("Escaped in %d minute(s).\n", ans);
  88. }
  89. return ;
  90. }

POJ2251 Dungeon Master —— BFS的更多相关文章

  1. POJ2251 Dungeon Master(bfs)

    题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...

  2. BFS POJ2251 Dungeon Master

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

  3. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  4. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  5. POJ2251——Dungeon Master(三维BFS)

    和迷宫问题区别不大,相比于POJ1321的棋盘问题,这里的BFS是三维的,即从4个方向变为6个方向. 用上队列的进出操作较为轻松. #include<iostream> #include& ...

  6. Dungeon Master bfs

    time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...

  7. poj 2251 Dungeon Master (BFS 三维)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  8. [poj] Dungeon Master bfs

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  9. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

随机推荐

  1. Linux(10):期中架构(2)--- NFS存储服务 & 实时同步

    1. 共享存储服务概念: # NFS是Network File System的缩写,中文意思是网络文件系统, # 它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录. 2. ...

  2. 线段树练习5(codevs 4927)

    题目描述 Description 有n个数和5种操作 add a b c:把区间[a,b]内的所有数都增加c set a b c:把区间[a,b]内的所有数都设为c sum a b:查询区间[a,b] ...

  3. Aragorn's Story(hdu3966)

    题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上的所有点权值加上K D C1 C2 K:把C1与C2的路径上的所有点权值减去K Q C:查询节点编号为C ...

  4. GDOI2018 新的征程

    看标题您一定以为考得很好.. Bad ending.想看美好结局的出门右转其他大佬博客. Day0 早上去车站的时候心情挺好.倒不是因为自己做足了准备,也不是因为预感到有好事发生,而是心情不好也没有用 ...

  5. angular中关于自定义指令——repeat渲染完成后执行动作

    业务中有时需要在异步获取数据并用ng-repeat遍历渲染完页面后执行某个操作,angular本身并没有提供监听ng-repeat渲染完成的指令,所以需要自己动手写.有经验的同学都应该知道,在ng-r ...

  6. Laravel 之Artisan

    简介: Artisan是Laravel中自带的命令行工具的名称: 由强大的Symfony Console组件驱动的: 提供了一些对应用开发有帮助的命令: 查看所有可用的Artisan的命令 php a ...

  7. dedecms--自定义session存值取值

    最近在用用dedecms开发项目,开发项目中遇到需要通过session存储信息在其他页面调取使用,但是对dedecms里面自带的session存储使用不好,我需要存储的是用户登录的时候信息,于是我就使 ...

  8. Django实现的博客系统中使用富文本编辑器ckeditor

    操作系统为OS X 10.9.2,Django为1.6.5. 1.下载和安装 1.1 安装 ckeditor 下载地址 https://github.com/shaunsephton/django-c ...

  9. Vijos——1359 Superprime

    Superprime 描述 农民约翰的母牛总是生产出最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还 ...

  10. MySQL 索引及其用法

    一.索引的作用 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,所以查询语句的优化显然是重中之重. 在数据 ...