您的位置:首页 > 其它

POJ - 2251 Dungeon Master(BFS)

2015-10-10 10:22 435 查看
题目大意:给你一个三维的迷宫,问能否逃出去

解题思路:纯BFS裸题

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 35;

struct Node{
int x, y, z, time;
Node() {}
Node(int x, int y, int z, int time): x(x), y(y), z(z), time(time) {}
};

char map

;
bool vis

;
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 0, -1}, {0, 0, 1}, {0, -1, 0}, {0, 1, 0}};//up, down, left, right, front, down
int L, R, C, sx, sy, sz, ex, ey, ez;

void init() {

for (int i = 1; i <= L; i++)
for (int j = 1; j <= R; j++) {
scanf("%s", map[i][j] + 1);
for (int k = 1; k <= C; k++)
if (map[i][j][k] == 'S') {
sx = i; sy = j; sz = k;
}
else if (map[i][j][k] == 'E') {
ex = i; ey = j; ez = k;
}
}
}

void solve() {
queue<Node> Q;
Q.push(Node(sx, sy, sz, 0));
memset(vis, 0, sizeof(vis));
vis[sx][sy][sz] = 1;
while (!Q.empty()) {
Node t = Q.front(); Q.pop();
int x = t.x;
int y = t.y;
int z = t.z;

if (x == ex && y == ey && z == ez) {
printf("Escaped in %d minute(s).\n", t.time);
return ;
}

for (int i = 0; i < 6; i++) {
int tx = x + dir[i][0];
int ty = y + dir[i][1];
int tz = z + dir[i][2];
if (tx <= 0 || tx > L || ty <= 0 || ty > R || tz <= 0 || tz > C || vis[tx][ty][tz] || map[tx][ty][tz] == '#') continue;
vis[tx][ty][tz] = 1;
Q.push(Node(tx, ty, tz, t.time + 1));
}
}
printf("Trapped!\n");
}

int main() {
while (scanf("%d%d%d", &L, &R, &C) != EOF && L + R + C) {
init();
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: