先是写了个DFS , RE,找不到答案,然后就bfs, 过了。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
char maze[33][33][33];
int vis[33][33][33];
int l, r, c, q[1000003];
int sx,sy,sz;
int move[7][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct p{int x, y, z;}path[1000003];
int bfs()
{
path[1].x = sx;
path[1].y = sy;
path[1].z = sz;
int front = 1, rear = 2;
while(front < rear)
{
for(int i = 0 ; i < 6; i++)
{
int px = path[front].x + move[i][0];
int py = path[front].y + move[i][1];
int pz = path[front].z + move[i][2];
if(px>=0 && px<l && py>=0 && py<r && pz>=0 && pz<c && maze[px][py][pz] != '#')
{
if(maze[px][py][pz] == 'E')
return q[front] + 1;
if(maze[px][py][pz] == '.' && !vis[px][py][pz])
{
vis[px][py][pz] = 1;
q[rear] = q[front] + 1;
path[rear].x = px;
path[rear].y = py;
path[rear].z = pz;
rear++;
}
}
}
front++;
}
return 0;
}
int main()
{
while(cin >> l >> r >> c, l+r+c)
{
getchar();
for(int i = 0; i < l; i++)
{
for(int j = 0; j < r; j++)
scanf("%s", maze[i][j]);
}
memset(vis, 0, sizeof(vis));
for(int i = 0; i < l; i++)
for(int j = 0; j < r; j++)
for(int k = 0; k < c; k++)
if(maze[i][j][k] == 'S')
{sx=i;sy=j;sz=k;break;}
int num = bfs();
if(num) printf("Escaped in %d minute(s).\n", num);
else cout << "Trapped!" << endl;
}
return 0;
}