#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<queue>
using namespace std;
int l, r, c,ans;
int map[32][32][32];
bool v[32][32][32];
int dl[6] = { 0, 0, 0, 0, 1, -1 };
int dx[6] = { -1, 0, 0, 1, 0, 0 };
int dy[6] = { 0, -1, 1, 0, 0, 0 };
struct point {
int l;
int x;
int y;
int s;
} beg;
queue<point> que;
int bfs() {
int ans = -1;
while (!que.empty()) {
point cnt = que.front();
que.pop();
if (map[cnt.l][cnt.x][cnt.y] == 'E') {
ans = cnt.s;
break;
}
for (int i = 0; i < 6; i++) {
point np = { cnt.l + dl[i], cnt.x + dx[i], cnt.y + dy[i], cnt.s + 1 };
if (np.l >= 0 && np.l < l&& np.x >= 0 && np.x < r && np.y >= 0 && np.y < c) {
if (map[np.l][np.x][np.y] != '#' && v[np.l][np.x][np.y] == 0) {
que.push(np);
v[np.l][np.x][np.y] = 1;
}
}
}
}
while (!que.empty()) {//清空队列
que.pop();
}
return ans;
}
int main() {
// freopen("data.in", "r", stdin);
while (scanf("%d%d%d", &l, &r, &c)) {
memset(v, 0, sizeof(v));
getchar();//吃掉回车
if (l == 0) {
return 0;
}
for (int i = 0; i < l; i++) {
for (int j = 0; j < r; j++) {
for (int k = 0; k < c; k++) {
map[i][j][k] = getchar();
if (map[i][j][k] == 'S') {
beg.l = i, beg.x = j, beg.y = k, beg.s = 0;
v[i][j][k] = 1;
}
}
getchar();//吃掉回车
}
getchar();//吃掉空行回车
}
que.push(beg);
int ans = bfs();
if (ans == -1) {
printf("Trapped!\n");
} else {
printf("Escaped in %d minute(s).\n", ans);
}
}
return 0;
}
POJ 2251 Dungeon Master(BFS)
最新推荐文章于 2019-08-02 19:19:16 发布