http://poj.org/problem?id=2251
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15203
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u
[Submit] [Go Back] [Status]
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
[Submit] [Go Back] [Status]
解析:
思路:裸的bfs
916 KB 32 ms C++ 1519 B 2
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char map[40][40][40];
int d[6][3]={{0,0,1},{0,0,-1},{-1,0,0},{0,1,0},{1,0,0},{0,-1,0,}};
int vis[40][40][40];
int que[40040][4];
int n,m,c,ans;
int sx,sy,sz;
int allow(int x,int y,int z)
{
if(x>=1&&x<=c&&y>=1&&y<=n&&z>=1&&z<=m&&map[x][y][z]!='#'&&vis[x][y][z]==0)
return 1;
else
return 0;
}
int bfs()
{
int front,rear;
que[0][0]=sx;
que[0][1]=sy;
que[0][2]=sz;
que[0][3]=0;
front=0;
rear=1;
vis[sx][sy][sz]=1;
while(front<rear)
{
int x,y,z,s;
x=que[front][0];
y=que[front][1];
z=que[front][2];
s=que[front++][3];
for(int i=0;i<6;i++)
{
int tx=x+d[i][0];
int ty=y+d[i][1];
int tz=z+d[i][2];
int ts=s+1;
if(allow(tx,ty,tz))
{
que[rear][0]=tx;
que[rear][1]=ty;
que[rear][2]=tz;
que[rear++][3]=ts;
vis[tx][ty][tz]=1;
if(map[tx][ty][tz]=='E')
{
ans=que[rear-1][3];
return 1;
}
}
}
}
return 0;
}
int main()
{
int i,j,k;
while(scanf("%d%d%d",&c,&n,&m)!=EOF)
{
if(n==0&&m==0&&c==0)
break;
for(i=1;i<=c;i++)
for(j=1;j<=n;j++)
{
for(k=1;k<=m;k++)
{
cin>>map[i][j][k];
if(map[i][j][k]=='S')
{
sx=i;
sy=j;
sz=k;
}
//cout<<map[i][j]<<endl;
}
}
//printf("%d %d %d ",sx,sy,sz);
ans=0;
memset(vis,0,sizeof(vis));
if(!bfs())
{printf("Trapped!\n");
}
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}