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

题目:

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!

  3. 题意:你处在一个三维的地牢里,从S出发逃到出口E,问最少要跑多远。
  4.  
  5. 思路:这题虽然是一个三维的地图,但是做法和二维的没多大区别,不过要从当前层到其他层的要求是你所在位置为非#,且你将到的那层的这个位置也是非#。
  6.  
  7. 代码实现如下:
  1. #include <queue>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int inf = 0x3f3f3f3f;
  7. int l, r, c, ans;
  8. int sx, sy, sz;
  9. char mp[][][];
  10. int vis[][][];
  11.  
  12. struct node {
  13. int x, y, z, step;
  14. }nw, nxt;
  15.  
  16. int dx[] = {, -, , , , }, dy[] = {, , , -, , },
  17. dz[] = {, , , , , -};
  18.  
  19. void bfs(int z, int x, int y) {
  20. vis[z][x][y] = ;
  21. nw.z = z, nw.x = x, nw.y = y, nw.step = ;
  22. queue<node> q;
  23. q.push(nw);
  24. while(!q.empty()) {
  25. nw = q.front(), q.pop();
  26. if(mp[nw.z][nw.x][nw.y] == 'E') {
  27. ans = nw.step;
  28. return;
  29. }
  30. for(int i = ; i < ; i++) {
  31. nxt.z = nw.z + dz[i];
  32. nxt.x = nw.x + dx[i];
  33. nxt.y = nw.y + dy[i];
  34. if(nxt.z >= && nxt.z < l && nxt.x >= && nxt.x < r && nxt.y >= && nxt.y < c && vis[nxt.z][nxt.x][nxt.y] == && mp[nxt.z][nxt.x][nxt.y] != '#') {
  35. nxt.step = nw.step + ;
  36. vis[nxt.z][nxt.x][nxt.y] = ;
  37. q.push(nxt);
  38. }
  39. }
  40. }
  41. }
  42.  
  43. int main() {
  44. while(~scanf("%d%d%d", &l, &r, &c) && (l + r + c)) {
  45. for(int i = ; i < l; i++) {
  46. for(int j = ; j < r; j++) {
  47. scanf("%s", mp[i][j]);
  48. for(int k = ; k < c; k++) {
  49. if(mp[i][j][k] == 'S') {
  50. sx = j, sy = k, sz =i;
  51. }
  52. }
  53. }
  54. }
  55. memset(vis, , sizeof(vis));
  56. ans = inf;
  57. bfs(sz, sx, sy);
  58. if(ans >= inf) {
  59. printf("Trapped!\n");
  60. } else {
  61. printf("Escaped in %d minute(s).\n", ans);
  62. }
  63. }
  64. return ;
  65. }
  1.  

Dungeon Master(三维bfs)的更多相关文章

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

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

  2. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  4. ZOJ 1940 Dungeon Master 三维BFS

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

  5. Dungeon Master(三维bfs)

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

  6. UVa532 Dungeon Master 三维迷宫

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

  7. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

  8. 棋盘问题(DFS)& Dungeon Master (BFS)

    1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...

  9. Dungeon Master (简单BFS)

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

  10. POJ 2252 Dungeon Master 三维水bfs

    题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...

随机推荐

  1. PART1 一些想法

    其实我一直是一个后知后觉的人,这点也是我过了好久才发现的问题,之所以晚发现自己这个毛病,是因为后知后觉==,这有点像是个悖论或者是笑话,但的确是真实存在于我的身上.其实当初为啥来这个学校选计算机的专业 ...

  2. 关于PHP使用GD库生成的验证码无法在别处显示

    https://segmentfault.com/q/1010000002522270

  3. iOS开发GCD的简单使用

    - (void)viewDidLoad { [super viewDidLoad]; // gcd 可以充分调用设备的 cpu 发挥最大性能,在 C 语言基础之上封装的 // dispatch_que ...

  4. 面试:谈谈你对Spring框架的理解

    Spring是一个优秀的轻量级框架,大大的提高了项目的开发管理与维护.Spring有两个核心模块.一个是IOC,一个是AOP. IOC: 就是控制反转的意思,指的是我们将对象的控制权从应用代码本身转移 ...

  5. openstack之horizon部署

    登录官网 www.openstack.org 查看安装文档 https://docs.openstack.org/newton/install-guide-rdo/horizon.html 第一步yu ...

  6. ES6装饰器Decorator基本用法

    1. 基本形式 @decorator class A {} // 等同于 class A {} A = decorator(A); 装饰器在javascript中仅仅可以修饰类和属性,不能修饰函数.装 ...

  7. [NOIP 2017]棋盘

    题目描述 有一个 m×m 的棋盘,棋盘上每一个格子可能是红色.黄色或没有任何颜色的.你现在要从棋盘的最左上角走到棋盘的最右下角. 任何一个时刻,你所站在的位置必须是有颜色的(不能是无色的), 你只能向 ...

  8. Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机

    Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机: #user nobody; worker_processes 1; #error_log logs/error.log; #err ...

  9. 数据添加到solr索引库后前台如何搜索

    主要结构: 查询 Dao: package com.taotao.search.dao.impl; import java.util.ArrayList; import java.util.List; ...

  10. SDWebImage的使用说明

    1. 在需要的地方导入头文件 #import "UIImageView+WebCache.h" webCache:网络缓存,几乎目前所有的浏览器都有一个内置的缓存,它们通常利用客户 ...