简单BFS在三维空间中使用BFS,还是比较简单了。直接定义出当前状态的相邻的六个面的相对坐标,直接搜索相临的状态即可。
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define MAX_N 31
const int INF = 0x3f3f3f3f;
int sx, sy ,sz;
int gx, gy, gz;
int l ,r ,c;
char maze[MAX_N][MAX_N][MAX_N];
bool vis[MAX_N][MAX_N][MAX_N];
struct node
{
int x, y, z, step;
node(int x, int y, int z, int step):x(x), y(y), z(z), step(step) {}
};
int mov[6][3] =
{
-1,0,0,
1,0,0,
0,1,0,
0,-1,0,
0,0,1,
0,0,-1
};
int bfs(int sx, int sy, int sz)
{
memset(vis, false, sizeof(vis));
queue<node> que;
vis[sx][sy][sz] = true;
que.push(node(sx, sy, sz, 0));
while(!que.empty())
{
node p = que.front();
que.pop();
if(maze[p.x][p.y][p.z] == 'E')
return p.step;
for(int i = 0 ; i < 6 ; i++)
{
int nx = p.x + mov[i][0];
int ny = p.y + mov[i][1];
int nz = p.z + mov[i][2];
if(!vis[nx][ny][nz] && nx >= 0 && ny >= 0 && nz >= 0 && nx < l && ny < r && nz < c && maze[nx][ny][nz] != '#')
{
vis[nx][ny][nz] = true;
que.push(node(nx, ny, nz, p.step + 1));
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d", &l, &r, &c) && ( l + r + c))
{
for(int i = 0 ; i < l ; i++)
{
getchar();
for(int j = 0 ; j < r ; j++)
{
for(int k = 0 ; k < c ; k++)
{
scanf("%c", &maze[i][j][k]);
if(maze[i][j][k] == 'S')
{
sx = i;
sy = j;
sz = k;
}
}
getchar();
}
}
int ans = bfs(sx, sy, sz);
if(ans == -1)
cout<<"Trapped!"<<endl;
else
printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}