题目链接: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. Spring(七)之基于注解配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  2. 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标 show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。

    这个题让我更加明白了类创建对象的实质 代码中用到:1.对象作形参;2.对象作返回值 以下用代码具体分析: class Point1{ public double x; public double y; ...

  3. Mysql查询正在运行的事务

    查询 正在执行的事务:SELECT * FROM information_schema.INNODB_TRX 根据这个事务的线程ID(trx_mysql_thread_id): 可以使用mysql命令 ...

  4. NIO高并发基础

    NIO高并发 是jdk1.4出现的新的流. NIO - New IO - 同步式非阻塞式IO BIO - Blocking IO - 同步式阻塞式IO ---UDP/TCP ==AIO - Async ...

  5. vue进入/离开 & 列表过渡transition

    一.transition过渡 1.需求1(优化):想要一种效果,想要ios那种页面切换效果,总而言之就是过渡效果. 附上官网介绍地址:https://cn.vuejs.org/v2/guide/tra ...

  6. eclipse安装activiti插件

    参考: https://blog.csdn.net/augustaurora/article/details/59618737 https://blog.csdn.net/qq_33547950/ar ...

  7. 在angular7中创建组件/自定义指令/管道

    在angular7中创建组件/自定义指令/管道 组件 使用命令创建组件 创建组件的命令:ng generate component 组件名 生成的组件组成: 组件名.html .组件名.ts.组件名. ...

  8. 浅谈 Virtual DOM 的那些事

    背景 我们都知道频繁的dom给我们带来的代价是昂贵的,例如我们有时候需要去更新Table 的部分数据,必须去重新重绘表格,这代价实在是太大了,相比于频繁的手动去操作dom而带来性能问题,vdom很好的 ...

  9. 初试mininet(可选PyCharm)

    目录 0x00 Mininet 0x01 Important classes, methods, functions 0x02 Sample 0x04 run in shell 0x05 Output ...

  10. CentOS6安装各种大数据软件 第十章:Spark集群安装和部署

    相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...