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!
理解
這道題是三維地圖廣搜鸿脓,我做這道題的時候遇到了很多麻煩:
一抑钟、開始用的深搜以為自己沒錯一直改改改,后來在學(xué)長的提醒下用bfs重新
寫了答憔;
二味赃、雖然不是大問題,但是在輸入的時候換行符的格式要注意虐拓,記得用getchar()吃掉心俗;
三、也是最重要的一點H鼐浴城榛!每個可行的分支在進(jìn)入隊列的時候一定要狀態(tài)標(biāo)記它!态兴!不然會重復(fù)運行導(dǎo)致超時狠持。
代碼部分
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
char map[31][31][31],cc;
int s1,s2,s3,x,y,z,a,b,c,finde,sum,fla[31][31][31];
struct node
{
int x,y,z;
};
queue<node>S;
node chan[6]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int main()
{
while(scanf("%d%d%d",&a,&b,&c)&&a&&b&&c)
{
getchar();//吃換行符
memset(fla,0,sizeof(fla));
memset(map,'#',sizeof(map));//數(shù)據(jù)清空
for(int i=0;i<a;i++)
{ for(int j=0;j<b;j++)
{ for(int k=0;k<c;k++)
{
scanf("%c",&map[i][j][k]);//地圖輸入
if(map[i][j][k]=='.'||map[i][j][k]=='E')
fla[i][j][k]=-1;
if(map[i][j][k]=='S')
{
s1=i;s2=j;s3=k;
}
}getchar();
}getchar();
}
node thes;
thes.x=s1;thes.y=s2;thes.z=s3;
while(!S.empty())//隊列清空
{
S.pop();
}
node count={-1,0,0};
S.push(count);
S.push(thes);
sum=-1;finde=0;
while(S.size()>1)//循環(huán)搜索出口
{
if(S.front().x == -1){ sum++; S.push(S.front()); S.pop(); }//層數(shù)標(biāo)記
x = S.front().x; y = S.front().y; z = S.front().z;
if(map[x][y][z]=='E'){finde=1;break;}
node si;
for(int kk=0;kk<6;kk++)
{
if(fla[x+chan[kk].x][y+chan[kk].y][z+chan[kk].z]==-1)
{
si.x=x+chan[kk].x;
si.y=y+chan[kk].y;
si.z=z+chan[kk].z;
S.push(si);
fla[x+chan[kk].x][y+chan[kk].y][z+chan[kk].z]=1;//一定要在這里標(biāo)記它!不然就可能超時瞻润。
}
}
S.pop();
}
if(finde==1)
{
printf("Escaped in %d minute(s).\n",sum);
}
else
printf("Trapped!\n");
}
return 0;
}