题目网址:POJ -- 2251
很典型的宽度优先搜索,尽管是一个三维的图,方法还是一模一样。 队列放入入口坐标,从入口坐标不断向外一层一层拓展,拓展到的元素进入队列并且标记该元素到入口的距离。
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 50;
int L, R, C;
int Sz, Sx, Sy;
int Ez, Ex, Ey;
char maze[maxn][maxn][maxn];
int dist[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
queue<int> q;
int bfs()
{
while(!q.empty()) q.pop();
q.push(Sz); q.push(Sx); q.push(Sy);
dist[Sz][Sx][Sy] = 0;
vis[Sz][Sx][Sy] = 1; /// 表示已在队列中
while(!q.empty())
{
int z = q.front(); q.pop();
int x = q.front(); q.pop();
int y = q.front(); q.pop();
if(z == Ez && x == Ex && y == Ey) return dist[z][x][y];
if(z + 1 <= L && maze[z+1][x][y] != '#' && !vis[z+1][x][y])
{
q.push(z+1); q.push(x); q.push(y);
dist[z+1][x][y] = dist[z][x][y] + 1;
vis[z+1][x][y] = 1;
}
if(z - 1 >= 1 && maze[z-1][x][y] != '#' && !vis[z-1][x][y])
{
q.push(z-1); q.push(x); q.push(y);
dist[z-1][x][y] = dist[z][x][y] + 1;
vis[z-1][x][y] = 1;
}
if(x + 1 <= R && maze[z][x+1][y] != '#' && !vis[z][x+1][y])
{
q.push(z); q.push(x+1); q.push(y);
dist[z][x+1][y] = dist[z][x][y] + 1;
vis[z][x+1][y] = 1;
}
if(x - 1 >= 1 && maze[z][x-1][y] != '#' && !vis[z][x-1][y])
{
q.push(z); q.push(x-1); q.push(y);
dist[z][x-1][y] = dist[z][x][y] + 1;
vis[z][x-1][y] = 1;
}
if(y + 1 <= C && maze[z][x][y+1] != '#' && !vis[z][x][y+1])
{
q.push(z); q.push(x); q.push(y+1);
dist[z][x][y+1] = dist[z][x][y] + 1;
vis[z][x][y+1] = 1;
}
if(y - 1 >= 1 && maze[z][x][y-1] != '#' && !vis[z][x][y-1])
{
q.push(z); q.push(x); q.push(y-1);
dist[z][x][y-1] = dist[z][x][y] + 1;
vis[z][x][y-1] = 1;
}
}
return 0;
}
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d%d%d", &L, &R, &C) && L)
{
memset(dist, 0, sizeof(dist));
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= L; i++)
for(int j = 1; j <= R; j++) {
scanf("%s", &maze[i][j][1]);
}
for(int i = 1; i <= L; i++)
for(int j = 1; j <= R; j++)
for(int k = 1; k <= C; k++)
if(maze[i][j][k] == 'S') { Sz = i; Sx = j; Sy = k; }
else if(maze[i][j][k] == 'E') { Ez = i; Ex = j; Ey = k; }
int dist = bfs();
if(dist) printf("Escaped in %d minute(s).\n", dist);
else printf("Trapped!\n");
}
return 0;
}
代码写得比较繁琐,bfs()中当前元素的相邻元素入队列的判定是一个一个手打上去的,其实可以构建一个数组d[][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}}; 这样把相邻坐标的计算写在循环中会更不宜出错。
一开始将g[][][]声明成了int型。。。犯过好几次这种错误了。存储图的原输入的时候一定记得用char型数组存储。
来源:oschina
链接:https://my.oschina.net/u/1780798/blog/634992