简单题
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 35
struct Point
{
int x, y, z;
} s, e;
int dir[6][3] =
{
{ 1, 0, 0 },
{ -1, 0, 0 },
{ 0, 1, 0 },
{ 0, -1, 0 },
{ 0, 0, 1 },
{ 0, 0, -1 } };
bool map[maxn][maxn][maxn];
int dist[maxn][maxn][maxn];
Point q[maxn * maxn * maxn];
int front, rear;
int nx, ny, nz;
void input()
{
memset(map, 0, sizeof(map));
getchar();
for (int i = 0; i < nz; i++)
{
for (int j = 0; j < nx; j++)
{
for (int k = 0; k < ny; k++)
{
char ch = getchar();
if (ch == '#')
map[j][k][i] = false;
else
map[j][k][i] = true;
if (ch == 'S')
{
s.z = i;
s.x = j;
s.y = k;
}
if (ch == 'E')
{
e.z = i;
e.x = j;
e.y = k;
}
}
getchar();
}
getchar();
}
}
bool check(int x, int y, int z)
{
if (x < 0 || y < 0 || z < 0 || x >= nx || y >= ny || z >= nz)
return false;
if (dist[x][y][z] != -1)
return false;
return map[x][y][z];
}
int bfs()
{
memset(dist, -1, sizeof(dist));
front = 0;
rear = 1;
q[0] = s;
dist[s.x][s.y][s.z] = 0;
while (front != rear)
{
Point a = q[front++];
if (front == maxn * maxn * maxn)
front = 0;
for (int i = 0; i < 6; i++)
{
int x = dir[i][0] + a.x;
int y = dir[i][1] + a.y;
int z = dir[i][2] + a.z;
if (check(x, y, z))
{
q[rear].x = x;
q[rear].y = y;
q[rear++].z = z;
if (rear == maxn * maxn * maxn)
rear = 0;
dist[x][y][z] = dist[a.x][a.y][a.z] + 1;
if (x == e.x && y == e.y && z == e.z)
return dist[x][y][z];
}
}
}
return -1;
}
int main()
{
// freopen("t.txt", "r", stdin);
while (scanf("%d%d%d", &nz, &nx, &ny), nx | ny | nz)
{
input();
int ans = bfs();
if (ans == -1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}