描述
你被困在一个3D地牢,需要找到最快的出路!地牢是由单位立方体组成,可以或不可以用岩石填充。将一个单位向北,南,东,西,上或下移动需要一分钟。你不能对角地移动,迷宫被四周的坚实岩石包围。
是逃生可能吗?如果是,需要多长时间?
输入
输入由多个地牢组成。每个地牢描述从包含三个整数L,R和C(大小都限制为30)的行开始。
L是组成地牢的等级数。
R和C是组成每个级别的计划的行和列的数量。
然后将跟随L个包含C个字符的R行块。每个字符描述地牢的一个单元格。充满岩石的细胞由’#’指示,空细胞由’。’表示。您的起始位置由“S”表示,出口由字母“E”表示。每个级别后面都有一个空白行。输入由L,R和C的三个零终止。
输出
每个迷宫生成一行输出。如果可以到达出口,打印一行表格
在x分钟内逸出。
其中x由逃脱所需的最短时间替代。
如果无法转义,请打印该行
被困!
#include<string.h>
#include<iostream>
using namespace std;
typedef class
{
public:
int l,r,c;
int depth; //树深(分钟)
}SE;
SE s,e;
bool maze[40][40][40];
int shortminute;
bool BFS(int k,int i,int j)
{
bool vist[40][40][40]={false};
SE queue[30000];
int head,tail;
queue[head=0].l=k;
queue[tail=0].r=i;
queue[0].c=j;
queue[tail++].depth=1;
vist[k][i][j]=true;
while(head<tail)
{
SE x=queue[head++];
if(x.l==e.l && x.r==e.r && x.c==e.c)
{
shortminute=x.depth;
return true;
}
if(maze[x.l][x.r][x.c-1] && !vist[x.l][x.r][x.c-1]) //West
{
vist[x.l][x.r][x.c-1]=true;
queue[tail].l=x.l;
queue[tail].r=x.r;
queue[tail].c=x.c-1;
queue[tail++].depth=x.depth+1;
}
if(maze[x.l][x.r-1][x.c] && !vist[x.l][x.r-1][x.c]) //North
{
vist[x.l][x.r-1][x.c]=true;
queue[tail].l=x.l;
queue[tail].r=x.r-1;
queue[tail].c=x.c;
queue[tail++].depth=x.depth+1;
}
if(maze[x.l][x.r][x.c+1] && !vist[x.l][x.r][x.c+1]) //East
{
vist[x.l][x.r][x.c+1]=true;
queue[tail].l=x.l;
queue[tail].r=x.r;
queue[tail].c=x.c+1;
queue[tail++].depth=x.depth+1;
}
if(maze[x.l][x.r+1][x.c] && !vist[x.l][x.r+1][x.c]) //South
{
vist[x.l][x.r+1][x.c]=true;
queue[tail].l=x.l;
queue[tail].r=x.r+1;
queue[tail].c=x.c;
queue[tail++].depth=x.depth+1;
}
if(maze[x.l-1][x.r][x.c] && !vist[x.l-1][x.r][x.c]) //Up
{
vist[x.l-1][x.r][x.c]=true;
queue[tail].l=x.l-1;
queue[tail].r=x.r;
queue[tail].c=x.c;
queue[tail++].depth=x.depth+1;
}
if(maze[x.l+1][x.r][x.c] && !vist[x.l+1][x.r][x.c]) //Down
{
vist[x.l+1][x.r][x.c]=true;
queue[tail].l=x.l+1;
queue[tail].r=x.r;
queue[tail].c=x.c;
queue[tail++].depth=x.depth+1;
}
}
return false;
}
int main(int i,int j,int k)
{
int L,R,C;
while(cin>>L>>R>>C)
{
if(!L && !R && !C)
break;
memset(maze,false,sizeof(maze));
for(k=1;k<=L;k++)
for(i=1;i<=R;i++)
for(j=1;j<=C;j++)
{
char temp;
cin>>temp;
if(temp=='.')
maze[k][i][j]=true;
if(temp=='S')
{
maze[k][i][j]=true;
s.l=k;
s.r=i;
s.c=j;
}
if(temp=='E')
{
maze[k][i][j]=true;
e.l=k;
e.r=i;
e.c=j;
}
}
if(BFS(s.l,s.r,s.c))
cout<<"Escaped in "<<shortminute-1<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
return 0;
}