E - DungeonMaster
TimeLimit:1000MS Description
You are trapped in a 3D dungeon and need to findthe quickest way out! The dungeon is composed of unit cubes whichmay or may not be filled with rock. It takes one minute to move oneunit north, south, east, west, up or down. You cannot movediagonally and the maze is surrounded by solid rock on allsides.
Is an escape possible? If yes, how long will ittake?
Is an escape possible? If yes, how long will ittake?
Input
The input consists of a number of dungeons. Eachdungeon 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 thedungeon.
R and C are the number of rows and columns making up the plan ofeach level.
Then there will follow L blocks of R lines each containing Ccharacters. Each character describes one cell of the dungeon. Acell full of rock is indicated by a '#' and empty cells arerepresented by a '.'. Your starting position is indicated by 'S'and the exit by the letter 'E'. There's a single blank line aftereach level. Input is terminated by three zeroes for L, R andC.
L is the number of levels making up thedungeon.
R and C are the number of rows and columns making up the plan ofeach level.
Then there will follow L blocks of R lines each containing Ccharacters. Each character describes one cell of the dungeon. Acell full of rock is indicated by a '#' and empty cells arerepresented by a '.'. Your starting position is indicated by 'S'and the exit by the letter 'E'. There's a single blank line aftereach level. Input is terminated by three zeroes for L, R andC.
Output
Each maze generates one line of output. If it ispossible to reach the exit, print a line of theform
where x is replaced by the shortest time it takes toescape.
If it is not possible to escape, print theline
Escaped in x minute(s).
where x is replaced by the shortest time it takes toescape.
If it is not possible to escape, print theline
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
#include
#include
#include
#include
using namespace std;
int dir[2]={1,-1};
char arr[30][30][30];
int l,r,c;
struct point
{
int row,col,level;
int step;
}p;
int bfs(point s,point e)
{
int map[r][c][l];
memset(map,0,sizeof(map));
queue que;
point p;
int x=0,y=0,z=0;
while(true)
{
if(s.col==e.col&&s.level==e.level&&s.row==e.row)
{
return s.step;
}
for(inti=0;i<6;i++)
{
if(i<2)
{
x=s.col+dir[i];
y=s.row;
z=s.level;
}
else if(i>=2&&i<4)
{
y=s.row+dir[i%2];
x=s.col;
z=s.level;
}
else if(i>=4&&i<6)
{
z=s.level+dir[i%2];
x=s.col;
y=s.row;
}
if(x<0||x>r-1||y<0||y>c-1||z<0||z>l-1)
continue;
if((!map[x][y][z])&&(arr[x][y][z]=='.'||arr[x][y][z]=='E'))
{
p.col=x;
p.level=z;
p.row=y;
map[x][y][z]=1;
p.step=s.step+1;
que.push(p);
}
}
if(que.empty())
return 0;
s=que.front();
que.pop();
}
}
int main()
{
point s;
point e;
while(cin>>l>>r>>c)
{
if(l==0&&r==0&&c==0)
{
break;
}
for(int k=0;k
{
for(inti=0;i
{
for(int j=0;j
{
cin>>arr[i][j][k];
if(arr[i][j][k]=='S')
{
s.col=i;
s.row=j;
s.level=k;
s.step=0;
}
if(arr[i][j][k]=='E')
{
e.col=i;
e.row=j;
e.level=k;
e.step=0;
}
}
}
}
int minute = bfs(s,e);
if(minute==0)
cout<<"Trapped!"<<endl;
else
{
cout<<"Escaped in "<<minute<<"minute(s)."<<endl;
}
}
return 0;
}
本文探讨了在3D迷宫中寻找最短路径的方法,包括输入格式、输出格式及解决算法,帮助玩家理解如何使用BFS算法进行路径搜索。
595

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



