题意:三维迷宫逃亡,六个方向。。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 10000005;
char map[40][40][40];
int vis[40][40][40];
int dis[MAXN];
int l,r,c,x,y,z;
int mx[6] = {1, -1, 0, 0, 0, 0};
int my[6] = {0, 0, 1, -1, 0, 0};
int mz[6] = {0, 0, 0, 0, 1, -1};
struct node
{
int x,y,z;
}path[MAXN];
int bfs()
{
dis[1] = 0 ;
path[1].x = x ;
path[1].y = y ;
path[1].z = z ;
memset(vis,false,sizeof(vis));
int front = 1,rear = 2 ;
while (front < rear)
{
for (int i = 0 ; i < 6 ; i++)
{
int nx = path[front].x + mx[i];
int ny = path[front].y + my[i];
int nz = path[front].z + mz[i];
if (nx>=0 && nx<r && ny>=0 && ny<c && nz>=0 && nz<l)
{
if (map[nz][nx][ny] == 'E')
return dis[front] + 1;
if (map[nz][nx][ny] == '.' && !vis[nz][nx][ny])
{
vis[nz][nx][ny] = 1 ;
dis[rear] = dis[front] + 1;
path[rear].x = nx ;
path[rear].y = ny ;
path[rear].z = nz ;
rear++;
}
}
}
front++;
}
return 0;
}
int main()
{
int t;
while (scanf("%d%d%d",&l,&r,&c) != EOF && l+r+c)
{
memset(map,0,sizeof(map));
for (int i = 0 ; i < l ; i++)
{
for (int j = 0 ; j < r ; j++)
{
getchar();
for (int k = 0 ; k < c ; k++)
{
scanf("%c",&map[i][j][k]);
if (map[i][j][k] == 'S')
{
x = j ;
y = k ;
z = i;
}
}
}
getchar();
}
t = bfs();
if (t)
printf("Escaped in %d minute(s).\n", t);
else printf("Trapped!\n");
}
return 0;
}