Dungeon Master - POJ 2251 - Virtual Judge
此题有坑,一开始以为只能从上往下走,结果是有可能从下一层的迷宫回到上一层的。
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 505;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
int L, R, C;
int ans;
int vis[35][35][35];
char s[35][35][35];
struct step {
int x, y, c, sp;
step(int tx, int ty, int tc, int ts): x(tx), y(ty), c(tc), sp(ts) {
}
};
int gox[6] = {0, 1, 0, 0, 0, -1};
int goy[6] = {1, 0, 0, 0, -1, 0};
int goc[6] = {0, 0, 1, -1, 0, 0};
int ec, ex, ey;
int bfs(int c, int x, int y) {
queue<step> que;
que.push(step(x, y, c, 0));
while (!que.empty()) {
step p = que.front();
que.pop();
//printf("%d %d %d %d\n", p.x, p.y, p.c, p.sp);
if (p.x == ex && p.y == ey && p.c == ec) {
return p.sp;
}
int tx, ty, tc;
for (int i = 0; i <= 5; i++) {
tx = p.x + gox[i];
ty = p.y + goy[i];
tc = p.c + goc[i];
if (tx >= 1 && tx <= R && ty >= 1 && ty <= C && tc >= 1 && tc <= L && s[tc][tx][ty] != '#' && vis[tc][tx][ty] == 0) {
vis[tc][tx][ty] = 1;
que.push(step(tx, ty, tc, p.sp + 1));
//printf();
}
}
}
return 0;
}
void solve() {
ans = 0;
memset(vis, 0, sizeof(vis));
int tx, ty, tc;
for (int i = 1; i <= L; i++) {
for (int j = 1; j <= R; j++) {
for (int k = 1; k <= C; k++) {
scanf(" %c", &s[i][j][k]);
if (s[i][j][k] == 'S') {
tc = i;
tx = j;
ty = k;
}
if (s[i][j][k] == 'E') {
ec = i;
ex = j;
ey = k;
}
}
}
}
int res = bfs(tc, tx, ty);
if (res) {
printf("Escaped in %d minute(s).\n", res);
} else {
printf("Trapped!\n");
}
}
int main() {
while (~scanf("%d %d %d", &L, &R, &C)) {
if (L == 0 && R == 0 && C == 0) {
break;
}
solve();
}
return 0;
}