POJ 2251 Dungeon Master (BFS)
Posted UNBROKEN
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2251 Dungeon Master (BFS)相关的知识,希望对你有一定的参考价值。
链接 : Here!
思路 : 三维的搜索, 只需要注意输入的图$G$, 第一为是 $z$ 即可. 剩下的就是普通的搜索, 只不过多加了一维而已...
/*************************************************************************
> File Name: E.cpp
> Author:
> Mail:
> Created Time: 2017年11月26日 星期日 10时51分05秒
************************************************************************/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
#define MAX_N 40
int L, R, C;
int dx[6] = {0, 0, -1, 1, 0, 0};
int dy[6] = {-1, 1, 0, 0, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};
int vis[MAX_N][MAX_N][MAX_N];
char G[MAX_N][MAX_N][MAX_N];
struct Point {
Point() {}
Point(int x, int y, int z, int step) : x(x), y(y), z(z), step(step) {}
int x, y, z;
int step;
};
Point st, ed;
void read() {
for (int k = 0 ; k < L ; ++k) {
getchar();
for (int i = 0 ; i < R ; ++i) {
scanf("%s", G[k][i]);
}
}
for (int k = 0 ; k < L ; ++k) {
for (int i = 0 ; i < R ; ++i) {
for (int j = 0 ; j < C ; ++j) {
if (G[k][i][j] == 'S') {
st.z = k; st.x = i; st.y = j;
st.step = 0;
}
if (G[k][i][j] == 'E') {
ed.z = k; ed.x = i; ed.y = j;
ed.step = 0;
}
}
}
}
}
int check(Point pt) {
if (pt.x < 0 || pt.x >= R || pt.y < 0 || pt.y >= C || pt.z < 0 || pt.z >= L) return 0;
if (vis[pt.x][pt.y][pt.z]) return 0;
if (G[pt.z][pt.x][pt.y] == '#') return 0;
return 1;
}
int BFS() {
queue<Point> que;
vis[st.x][st.y][st.z] = 1;
que.push(st);
while (!que.empty()) {
Point now = que.front();
que.pop();
if (now.x == ed.x && now.y == ed.y && now.z == ed.z) {
return now.step;
}
for (int i = 0 ; i < 6 ; ++i) {
Point temp(now.x + dx[i],
now.y + dy[i],
now.z + dz[i],
now.step + 1);
if (!check(temp)) continue;
vis[temp.x][temp.y][temp.z] = 1;
que.push(temp);
}
}
return -1;
}
void solve() {
int ret = BFS();
if (ret != -1) {
printf("Escaped in %d minute(s).\n", ret);
} else {
printf("Trapped!\n");
}
}
int main() {
// freopen("./in.in", "r", stdin);
while (scanf("%d%d%d", &L, &R, &C) != EOF) {
if (L == 0 && R == 0 && C == 0) break;
memset(vis, 0, sizeof(vis));
memset(G, 0, sizeof(G));
read();
solve();
}
return 0;
}
以上是关于POJ 2251 Dungeon Master (BFS)的主要内容,如果未能解决你的问题,请参考以下文章