博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2251——Dungeon Master(BFS)
阅读量:2343 次
发布时间:2019-05-10

本文共 3003 字,大约阅读时间需要 10 分钟。

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!

多了一个需要考虑层数的情况,但也只是多了一种入队条件罢了,没什么很难的。但我当时没想到还可以向上走就死命WA。。。。要思考得更全面啊

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXN 35#define mod 2012#define INF 0x3f3f3f3fusing namespace std;int l,r,c;bool flag;char m[MAXN][MAXN][MAXN];int vis[MAXN][MAXN][MAXN];struct Node{ int x,y,z,step;};void bfs(int x,int y,int z){ Node tmp,tmp2,tmp3; tmp.x=x; tmp.y=y; tmp.z=z; tmp.step=0; queue
q; q.push(tmp); while(!q.empty()) { tmp2=q.front(); q.pop(); if(m[tmp2.z][tmp2.x][tmp2.y]=='E') { flag=true; cout<<"Escaped in "<
<<" minute(s)."<
=0) { tmp3.x=tmp2.x-1; tmp3.y=tmp2.y; tmp3.z=tmp2.z; tmp3.step=tmp2.step; if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y]) { vis[tmp3.z][tmp3.x][tmp3.y]=1; tmp3.step++; q.push(tmp3); } } if(tmp2.y+1
=0) { tmp3.x=tmp2.x; tmp3.y=tmp2.y-1; tmp3.z=tmp2.z; tmp3.step=tmp2.step; if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y]) { vis[tmp3.z][tmp3.x][tmp3.y]=1; tmp3.step++; q.push(tmp3); } } if(tmp2.z+1
=0) { tmp3.x=tmp2.x; tmp3.y=tmp2.y; tmp3.z=tmp2.z-1; tmp3.step=tmp2.step; if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y]) { vis[tmp3.z][tmp3.x][tmp3.y]=1; tmp3.step++; q.push(tmp3); } } }}int main(){ ios::sync_with_stdio(false); while(cin>>l>>r>>c) { if(l==0&&r==0&&c==0) break; memset(m,'#',sizeof(m)); int x,y,z; for(int k=0; k
>m[k][i][j]; if(m[k][i][j]=='S') { x=i; y=j; z=k; } } memset(vis,0,sizeof(vis)); flag=false; bfs(x,y,z); if(!flag) cout<<"Trapped!"<

转载地址:http://uicvb.baihongyu.com/

你可能感兴趣的文章
tsung集群测试
查看>>
oracle定时删除表空间的数据并释放表空间
查看>>
解决文件提示: /bin/ksh^M: bad interpreter: bad interpreter:No such file or directory
查看>>
ajaxanywhere jsp 使用
查看>>
jquery的使用
查看>>
如何静态化JSP页面
查看>>
XML 与 Java 技术: 用 Castor 进行数据绑定
查看>>
Python未知领域系列:(附Python学习教程+Python学习路线)Python高级教程之面向对象
查看>>
盘点Python 面向对象编程最容易被忽视的知识点
查看>>
Python:一个可以套路别人的python小程序
查看>>
用Python告诉你:这些年,我们点过的的那些外卖
查看>>
如何美观地打印Python对象?这个标准库可以简单实现
查看>>
写作路上的这些小成绩,铸就了一个不平庸的程序员
查看>>
程序员找工作的个人经验教训以及注意事项
查看>>
2019 编程语言排行榜:Java、Python 龙争虎斗!谁又屹立不倒
查看>>
拥有10年编程经验的你,为什么还一直停留在原地
查看>>
Flask vs Django,Python Web开发用哪个框架更好
查看>>
用Python制作动态二维码,一行代码就做到了
查看>>
Python说:常见的数据分析库有哪些
查看>>
Python教程:Python数据类型之字典
查看>>