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

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 51402   Accepted: 19261

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! 题意: 在一个三维空间中,# 表示石块,不能走,. 表示路,可以走,求从 S 到 E 花费的最短时间,每走一步花费时间加 1 。
注意:
1.不要写 if(bfs()) printf("%d",bfs()),这种形式,要写一个中间变量 int ans = bfs() ,然后,if(ans) printf("%d",ans);
2.这是一个三维空间,有六个方向
3.不要只判断当前位置是不是被标记过,还要判断当前位置是不是石头(“#”);
 #include<iostream>
#include<queue>
#include <cstring>
#include<cstdio> using namespace std; const int maxn = ;
int go[][] = {,,,,,-,-,,,,,,,,,,-,};
bool mark[maxn][maxn][maxn];
char maze[maxn][maxn][maxn];
int sx,sy,sz,ex,ey,ez;
int L,R,C;
struct node
{
int x,y,z;
int time;
}; bool Isok(int x,int y,int z)
{
//别忘了判断是不是'#'
return x >= && x < L && y >= && y < R && z >= && z < C && !mark[x][y][z] && maze[x][y][z] != '#';
}
int bfs()
{
queue<node> q;
struct node cu,ne;
cu.x=sx;cu.y=sy;cu.z=sz;
cu.time = ;
mark[sx][sy][sz]=;
q.push(cu);
while(!q.empty())
{
cu = q.front();
q.pop();
if(cu.x==ex&&cu.y==ey&&cu.z==ez)
return cu.time;
for(int i=;i<;i++)
{
ne.x = cu.x + go[i][];
ne.y = cu.y + go[i][];
ne.z = cu.z + go[i][];
if(Isok(ne.x,ne.y,ne.z))
{
mark[ne.x][ne.y][ne.z] = ;
ne.time = cu.time + ;
q.push(ne);
}
}
}
return ;
}
int main()
{
while(~scanf("%d%d%d",&L,&R,&C)&&(L||R||C))
{
memset(mark,,sizeof(mark));
memset(maze,,sizeof(maze));
for(int i=;i<L;i++)
{
for(int j=;j<R;j++)
scanf("%s",maze[i][j]);
getchar(); //有没有getchar()都能过
}
for(int i=;i<L;i++)
{
for(int j=;j<R;j++)
{
for(int k=;k<C;k++)
{
if(maze[i][j][k] == 'S')
{
sx=i;sy=j;sz=k;
}
else if(maze[i][j][k] == 'E')
{
ex=i;ey=j;ez=k;
}
}
}
}
//不要直接使用bfs()的返回值做判断和做printf输出变量。
int ans = bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return ;
}

Ac代码

POJ 2251 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 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...

  3. (简单) POJ 2251 Dungeon Master,BFS。

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

  4. poj 2251 Dungeon Master(bfs)

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

  5. POJ 2251 Dungeon Master【BFS】

    题意:给出一个三维坐标的牢,给出起点st,给出终点en,问能够在多少秒内逃出. 学习的第一题三维的广搜@_@ 过程和二维的一样,只是搜索方向可以有6个方向(x,y,z的正半轴,负半轴) 另外这一题的输 ...

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

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

  7. BFS POJ 2251 Dungeon Master

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

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

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

  9. 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. .net根据经纬度获取地址(百度api)

    private string GetAddress(string lng, string lat) { try { string url = @"http://api.map.baidu.c ...

  2. 阿里云阿里免费ssl wap网站在手机微信、手机浏览器无法访问

    图片可以访问,样式无法显示 https://css.cnbuses.com/css/wap/gonggong.css 1,怀疑是开了代理访问. 关闭后还是访问空白. 2.在手机浏览器打开,提示该网站的 ...

  3. linux 编译ffmpeg 支持x264, x265

    1. 前言 本教程涉及的ffmpeg, x264, x265 2. 环境依赖 2.1 删除系统中安装的ffmpeg等库 sudo apt-get --purge remove ffmpeg mplay ...

  4. linux的虚拟文件系统VFS

    虚拟文件系统(virtual file system),别名虚拟文件系统开关,是linux中的一个软件层,向用户空间提供文件系统操作接口. VFS包含的系统调用包括open(2).stat(2).re ...

  5. git did not exit cleanly (exit code 1)

    git pull的时候报如下错误: error: Your local changes to the following files would be overwritten by merge: 文件 ...

  6. SQL Server 数据库空间使用情况

    GO /****** Object: StoredProcedure [dbo].[SpaceUsed] Script Date: 2017-12-01 11:15:11 ******/ SET AN ...

  7. 3.高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建

    高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建 如果大家看了我的上一篇<2.高并发教程-基础篇-之nginx+mysql实现负载均衡和读写分离>文章,如果能很好的 ...

  8. oracle基础知识过一遍(原创)

    用户.角色.权限.表空间 create tablespace test1_tablespace datafile ‘test1file.dbf’ size 10m; create temporary  ...

  9. Vue聊天框默认滚动到底部

    功能场景 在开发中,我们总能遇到某些场景需要运用到聊天框,比如客服对话.如果你不是一名开发人员,可能你在使用QQ或者聊天工具的时候并没有注意到,当你发出一条消息的时候,窗体会默认滚动到最底部,让用户可 ...

  10. js获取当前时间并转化

    1.转化为  年月日 function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var ...