Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28093 Accepted: 10969
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!
Source
Ulm Local 1997
很水 就是个多层迷宫问题bfs一下即可。注意一下姿势不能走的情况不要塞到队列里,提前判断掉,不然会MLE或者TLE..不要问我是怎么知道。。。
#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
#include "queue"
using namespace std;
char mmap[35][35][35];
bool vis[35][35][35];
int sl,sr,sc;
int l,r,c;
int ans;
typedef struct node
{
int l,r,c,step;
}node;
queue<node> list;
inline bool checkk(node next)
{
if(vis[next.l][next.r][next.c]==0&&mmap[next.l][next.r][next.c]!='#')
return true;
else
return false;
}
void bfs()
{
vis[sl][sr][sc]=1;
node first;
first.l=sl;
first.r=sr;
first.c=sc;
first.step=0;
list.push(first);
while(!list.empty())
{
node now=list.front();
list.pop();
vis[now.l][now.r][now.c]=1;
if(mmap[now.l][now.r][now.c]=='E')
{
ans=now.step;
return;
}
node next=now;
if(now.r+1<=r)
{
next=now;
next.step++;
next.r++;
if(checkk(next))
{
list.push(next);
vis[next.l][next.r][next.c]=1;
}
}
if(now.r-1>=1)
{
next=now;
next.step++;
next.r--;
if(checkk(next))
{
list.push(next);
vis[next.l][next.r][next.c]=1;
}
}
if(now.c+1<=c)
{
next=now;
next.step++;
next.c++;
if(checkk(next))
{
list.push(next);
vis[next.l][next.r][next.c]=1;
} }
if(now.c-1>=1)
{
next=now;
next.step++;
next.c--;
if(checkk(next))
{
list.push(next);
vis[next.l][next.r][next.c]=1;
}
}
if(now.l+1<=l)
{
next=now;
next.l++;
next.step++;
if(checkk(next))
{
list.push(next);
vis[next.l][next.r][next.c]=1;
}
}
if(now.l-1>=1)
{
next=now;
next.l--;
next.step++;
if(checkk(next))
{
list.push(next);
vis[next.l][next.r][next.c]=1;
}
}
}
}
int main()
{
while(~scanf("%d%d%d",&l,&r,&c))
{
ans=-1;
while(!list.empty())
list.pop();
memset(vis,0,sizeof(vis));
if((l+r+c)==0)
return 0;
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
scanf("%s",mmap[i][j]+1);
for(int k=1;k<=c;k++)
{
if(mmap[i][j][k]=='S')
{
sl=i;sr=j;sc=k;
}
}
}
}
bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}