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?
Is an escape possible? If yes, how long will it take?
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.
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
三维迷宫问题,忘记考虑时间问题了,一开始用的dfs,然后超时了,改成bfs后就好了。
代码如下
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char s[35][35][35];
int x1,x2,y1,y2,z1,z2;
int sum;
int l,r,c;
int vis[35][35][35];
int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int judge(int z,int x,int y)
{
if(x<1||y<1||z<1||z>l||x>r||y>c)
return 0;
if(vis[z][x][y])
return 0;
if(s[z][x][y]=='#')
return 0;
vis[z][x][y]=1;
return 1;
}
struct Node
{
int z;
int x;
int y;
int step;
};
void bfs(int z,int x,int y)
{
queue<Node>que;
Node node;
node.z=z;
node.x=x;
node.y=y;
node.step=0;
que.push(node);
while(!que.empty())
{
Node temp;
temp=que.front();
que.pop();
for(int i=0;i<6;++i)
{
int tz=temp.z+dir[i][0];
int tx=temp.x+dir[i][1];
int ty=temp.y+dir[i][2];
if(judge(tz,tx,ty))
{
Node t;
t.z=tz;
t.x=tx;
t.y=ty;
t.step=temp.step+1;
que.push(t);
if(tz==z2&tx==x2&&ty==y2)
{
sum=min(sum,t.step);
return ;
}
}
}
}
}
int main()
{
while(cin>>l>>r>>c&&r)
{
sum=1000000;
memset(vis,0,sizeof(vis));
for(int i=1;i<=l;++i)
{
for(int j=1;j<=r;++j)
scanf("%s",s[i][j]+1);
}
for(int i=1;i<=l;++i)
for(int j=1;j<=r;++j)
for(int k=1;k<=c;++k)
{
if(s[i][j][k]=='S')
{
z1=i,x1=j,y1=k;
}
if(s[i][j][k]=='E')
{
z2=i,x2=j,y2=k;
}
}
vis[z1][x1][y1]=1;
bfs(z1,x1,y1);
if(sum==1000000)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<sum<<" minute(s)."<<endl;
}
}
本文介绍了一个三维迷宫问题,通过使用BFS算法寻找从起点到终点的最短路径。问题描述了一个由单位立方体组成的三维迷宫,玩家需要找到最快的方式逃出。文章提供了完整的C++代码实现。
Dungeon Master&spm=1001.2101.3001.5002&articleId=70917803&d=1&t=3&u=62b309b797a7445b833b03b190423adc)
322

被折叠的 条评论
为什么被折叠?



