题目: http://poj.org/problem?id=2251
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; char maze[][][];
bool vis[][][];
int dir[][] = {{,,}, {,,}, {,,}, {,,-}, {,-,}, {-,,}}; struct Point
{
int x, y, z, step;
}; struct Point start; queue<struct Point>q;
void bfs()
{
while(!q.empty())q.pop();
memset(vis, , sizeof(vis));
q.push(start);
vis[start.x][start.y][start.z] = ;
while(!q.empty())
{
struct Point u = q.front();
q.pop();
if(maze[u.x][u.y][u.z] == 'E')
{
printf("Escaped in %d minute(s).\n", u.step);
return;
}
for(int d = ; d < ; d++)
{
int nx = u.x + dir[d][];
int ny = u.y + dir[d][];
int nz = u.z + dir[d][];
if(maze[nx][ny][nz] != '#' && maze[nx][ny][nz] != && !vis[nx][ny][nz])
{
q.push((struct Point){nx, ny, nz, u.step+});
vis[nx][ny][nz] = ;
}
}
}
printf("Trapped!\n");
} int main()
{
int a, b, c;
while(scanf("%d %d %d", &a, &b, &c) != EOF)
{
if(a == && b == && c == )break;
memset(maze, , sizeof(maze));
for(int i = ; i <= a; i++)
{
for(int j = ; j <= b; j++)
{
scanf("%s", &maze[i][j][]);
for(int k = ; k <= c; k++)
{
if(maze[i][j][k] == 'S')
start = (struct Point){i, j, k, };
}
}
}
bfs();
}
return ;
}