Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32228   Accepted: 12378

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

3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped! 普通的bfs迷宫题,只是增加了第3维。
 #include<iostream>
#include<queue>
#include<cstring>
using namespace std; const int MAX=;
struct Node
{
char c;
int pace,z,x,y;//z,x,y--楼层,行,列
}maze[MAX][MAX][MAX];//存储迷宫字符,坐标,到达步数
int L,R,C;
int dre[][]={{,,},{-,,},{,,},{,-,},{,,},{,,-}};//方向:东南西北上下
bool check(int z,int x,int y)//检查坐标是否超出范围
{
return (z>=&&z<=L&&x>=&&x<=R&&y>=&&y<=C);
}
int bfs(int z0,int x0,int y0)//找到从点[z0][x0][y0]到终点的最小步数
{
queue<Node> Q;
Node t;
Q.push(maze[z0][x0][y0]);//放入起点
while(!Q.empty())
{
t=Q.front();
Q.pop();
if(t.c=='E') return t.pace;//找到,返回步数
for(int i=;i<;i++)//枚举6个方向,若可以走且未走过就push到队尾
{
int Z=t.z+dre[i][],X=t.x+dre[i][],Y=t.y+dre[i][];
if(check(Z,X,Y))
{
if(maze[Z][X][Y].c!='#'&&!maze[Z][X][Y].pace)
{
maze[Z][X][Y].pace=t.pace+;//步数等于上一步(t)的步数加1
Q.push(maze[Z][X][Y]);
}
}
}
}
return -;//找不到返回-1
}
int main()
{
while(cin>>L>>R>>C,L||R||C)
{
int sz,sx,sy;
memset(maze,,sizeof(maze));
for(int i=; i<L; i++)
for(int j=; j<R; j++)
for(int k=; k<C; k++)
{
cin>>maze[i][j][k].c;
maze[i][j][k].z=i;
maze[i][j][k].x=j;
maze[i][j][k].y=k;
if(maze[i][j][k].c=='S')//记录起点
{
sz=i;
sx=j;
sy=k;
}
}
int re=bfs(sz,sx,sy);
if(re!=-) cout<<"Escaped in "<<re<<" minute(s).\n";
else cout<<"Trapped!\n";
}
return ;
}

bfs—Dungeon Master—poj2251的更多相关文章

  1. Dungeon Master POJ-2251 三维BFS

    题目链接:http://poj.org/problem?id=2251 题目大意 你被困在了一个三维的迷宫,找出能通往出口的最短时间.如果走不到出口,输出被困. 思路 由于要找最短路径,其实就是BFS ...

  2. BFS POJ2251 Dungeon Master

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

  3. POJ2251 Dungeon Master —— BFS

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

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

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

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

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

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

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

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

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

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

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

  9. POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48380   Accepted: 18252 ...

随机推荐

  1. 部署zookeepe高可用集群

                                                                部署zookeepe高可用集群 部署规划 Nno1         192.16 ...

  2. RedHat 6.5 上将系统语言修改为中文

    RedHat 6.5 上将系统语言修改为中文 1.打开终端,输入su -,键入密码,获取超级用户权限. 2.输入cd /etc/sysconfig,进入设置目录. 3.输入vi i18n,进入到配置文 ...

  3. django-rest-framework视图和url

    django-rest-framework视图 GenericView class GenericView(GenericAPIView): queryset = models.Role.object ...

  4. canvas压缩、裁切图片和格式转换的方法

    按照大小压缩图片,或者按照特定分辨率裁切图片,转为blob数据.自动处理ios中可能存在的照片偏差90°问题. 例如,获取300*300大小的头像,实现以下效果: 使用方式: <!-- 引入js ...

  5. Powershell 输出信息过多,结尾显示省略号

    有时候我们通过powershell指令去查询某些信息时,因为输出结果过多,导致一部分重要信息被省略号代替,如下图 面对这种情况无论是 |fl 还是  out-file 亦或是 export-csv都无 ...

  6. Linux C++ 网络编程学习系列(6)——多路IO之epoll高级用法

    poll实现多路IO 源码地址:https://github.com/whuwzp/linuxc/tree/master/epoll_libevent 源码说明: server.cpp: 监听127. ...

  7. std::string构造函数

    string(); string (const string& str); string (const string& str, size_t pos, size_t len = np ...

  8. 在Sping的配置文件中,关于dataSource的配置,就我们常用的方法大致可以有三种:

    在Sping的配置文件中,关于dataSource的配置,就我们常用的方法大致可以有三种: 1.一般的配置方法,直接在配置中指定其值.具体的例子我们参照Mysql的配置如下: <bean id= ...

  9. AJ学IOS(08)UI之热门_喜马拉雅UI实现-UIScrollView的使用

    AJ分享,必须精品 先看效果 storyBoard用到的控件 代码实现 */ // // NYViewController.m // 05 - 喜马拉雅 // // Created by apple ...

  10. 五分钟!用python绘制漂亮的系统架构图

    Diagrams 是一个基于Python绘制云系统架构的模块,它能够通过非常简单的描述就能可视化架构,并支持以下6个云产品的图标: AWS.Azure.GCP.K8s.阿里云 和 Oracle 云 基 ...