Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11809 | Accepted: 4574 |
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?
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
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!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
Source
一道3维的广搜题目。模板下,易过,队列还是有点不熟,所以用数组模拟了。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
struct move
{
int l,r,c;
int min;
}s,e,mov[30000];
char map[40][40][40];
int l,r,c,ans;
bool vis[40][40][40];
int str[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int bfs()
{
int L,R,C;
int front=0,rear=1;
mov[front].c=s.c;
mov[front].l=s.l;
mov[front].r=s.r;
vis[s.l][s.r][s.c]=true;
while(front!=rear)//可以用队列,这是用数组模拟队列的过程,容易理解。如果为队列的话则为while(!q.empty())
{
for(int i=0;i<6;i++)
{
L=mov[front].l+str[i][0];
R=mov[front].r+str[i][1];
C=mov[front].c+str[i][2];
// printf("%d %d %d %d %c\n",L,R,C,vis[L][R][C],map[L][R][C]);
if(L>=0&&L<l&&R>=0&&R<r&&C>=0&&C<c&&!vis[L][R][C]&&map[L][R][C]!='#')//边界
{
if(L==e.l&&R==e.r&&C==e.c)
{
ans=mov[front].min+1;
return 1;
}
mov[rear].l=L;
mov[rear].r=R;
mov[rear].c=C;
vis[L][R][C]=1;
mov[rear].min=mov[front].min+1;
rear++;
}
}
front++;
}
return 0;
}
int main()
{
int i,j,k;
while(cin>>l>>r>>c,c+l+r)
{
memset(vis,false,sizeof(vis));
for(i=0;i<l;i++)
for(j=0;j<r;j++)
for(k=0;k<c;k++)
{
cin>>map[i][j][k];
if(map[i][j][k]=='S')
{
s.l=i;
s.r=j;
s.c=k;
}
if(map[i][j][k]=='E')
{
e.l=i;
e.r=j;
e.c=k;
}
}
if(bfs())
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
}