POJ 2251-Dungeon Master (三维空间求最短路径)
2024-09-30 10:53:00
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.
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求。
#include<cstdio>
#include<queue>
using namespace std;
int dx[]={-,,,,,};
int dy[]={,,-,,,};
int dz[]={,,,,,-};
char str[][][];
int l,r,c,i,j,k,ans,bx,bz,by;
struct stu
{
int x,y,z,step;
};
bool f(stu st)
{
if(st.x< || st.z< || st.y< || st.x>=r || st.y>=c || st.z>=l || str[st.z][st.x][st.y] =='#')
{
return false;
}
return true;
}
int bfs(int bz,int bx,int by)
{
str[bz][bx][by]='#';
int nx,ny,time,zz,xx,yy;
queue<stu> que;
stu st,next;
st.x=bx;
st.y=by;
st.z=bz;
st.step=;
que.push(st); while(!que.empty())
{ st=que.front();
que.pop(); xx=st.x;
yy=st.y;
zz=st.z;
time=st.step;
for(i = ;i < ; i++)
{
st.x=xx+dx[i];
st.y=yy+dy[i];
st.z=zz+dz[i];
if(!f(st))
{
continue;
}
if(str[st.z][st.x][st.y] == 'E')
{
return time+;
} st.step=time+;
que.push(st);
str[st.z][st.x][st.y]='#';
}
}
return ;
}
int main()
{
while(scanf("%d %d %d",&l,&r,&c)&&l&&r&&c)
{
for(i = ; i < l ; i++)
{
for(j = ; j < r ;j++)
{
scanf("%s",str[i][j]);
for(k = ; k < c ; k++)
{
if(str[i][j][k] == 'S')
{
bz=i;
bx=j;
by=k;
}
}
}
}
ans=bfs(bz,bx,by);
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n"); }
}
最新文章
- 会话控制(session、cookie)
- Mac OS 快捷键
- 趣谈PHP 多态
- HDU1166-敌兵布阵(线段树)
- Android中 判断是平板还是手机
- java/php/c#版rsa签名以及java验签实现--转
- Android Studio,使用外部模拟器作为生成app调试的模拟器
- Hibernate HQL中的子查询
- PHPCMS v9.5.6 通杀getshell(前台)
- 关于jmeter读取CSV文件的详细设置
- Ansoftmaxwell15.0
- mobike
- JS获取宽度高度大集合
- MongoDB数据库基本命令
- 关于UIPageViewController那些事
- python简单实现队列和栈push、pop操作
- ILP32、ILP64、LP64、LLP64、64位系统
- 聊聊Java中的拆箱和装箱操作
- 2016年第七届蓝桥杯C/C++B组省赛题目解析
- 题目1003:A+B