题目链接:http://poj.org/problem?id=2251
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….
.###.
.##..
###.######
#####
##.##
##…#####
#####
#.###
####E1 3 3
S##
#E#
###0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
Source
Ulm Local 1997
题意
环境是一个三维的图,只能在‘.’上走,可以上下左右前后走,求从S走到E的最短距离。
解题思路
相当于一个三维的图论最短路,但是边权值都为1,所以可以选择BFS,也可以选择SPFA等经典最短路径求解方法。对于这种最短路问题来说,BFS永远要优于DFS,因为DFS是要把所有的路都走一遍,然后取最优值,但是BFS只会用最优的方法走到目标点,再远的路径就不会重复了,所以时间会快很多。这道题目我当时用DFS就超时了,后来改成了BFS才能过。
AC代码
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
int r,c,l,vis[35][35][35],ans;
char a[35][35][35];
struct node{
int x,y,z,dis;
}Begin,End;
struct Dir{
int x,y,z;
Dis(int _x=0,int _y=0,int _z=0):x(_x),y(_y),z(_z){}
}dir[6];
queue<node> q;
int bfs()
{
int x,y,z;
while(q.size()){
node now=q.front();
q.pop();
//cout<<now.x<<" "<<now.y<<' '<<now.z<<' '<<now.dis<<"$$$"<<endl;
if(now.x==End.x&&now.y==End.y&&now.z==End.z){
return now.dis;
}
for(int i=0;i<6;i++){
x=now.x+dir[i].x;
y=now.y+dir[i].y;
z=now.z+dir[i].z;
if(x>=0&&x<r&&y>=0&&y<c&&z>=0&&z<l){
if(!vis[x][y][z]&&a[x][y][z]!='#'){
vis[x][y][z]=1;
node tmp;
tmp.x=x;
tmp.y=y;
tmp.z=z;
tmp.dis=now.dis+1;
q.push(tmp);
}
}
}
}
return -1;
}
int main()
{
char str[36];
dir[0]=Dir(1,0,0);
dir[1]=Dir(0,1,0);
dir[2]=Dir(-1,0,0);
dir[3]=Dir(0,-1,0);
dir[4]=Dir(0,0,1);
dir[5]=Dir(0,0,-1);
int id,st,now;
while(~scanf("%d%d%d",&l,&r,&c)&&r&&c&&l){
memset(vis,0,sizeof(vis));
for(int k=0;k<l;k++){
for(int i=0;i<r;i++){
scanf("%s",str);
for(int j=0;j<c;j++){
a[i][j][k]=str[j];
if(str[j]=='S'){
Begin.x=i;
Begin.y=j;
Begin.z=k;
Begin.dis=0;
}
else if(str[j]=='E'){
End.x=i;
End.y=j;
End.z=k;
End.dis=0;
}
}
}
}
while(q.size()) q.pop();
q.push(Begin);
vis[Begin.x][Begin.y][Begin.z]=1;
ans=bfs();
if(ans==-1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}