POJ - 2251 Dungeon Master(BFS)

题目大意:给你一个三维的迷宫,问能否逃出去

解题思路:纯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[N][N][N];
bool vis[N][N][N];
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值