UVA - 532 Dungeon Master

题目大意:逃出地牢。L 层 长 R 宽 C,从 S 走到 E 要移动几次(秒),无法逃出输出 Trapped!

解题思路:. 可走,# 不能走,化为 0 1,0 可走 1 不可走,走过的位置标记为 1。仿照上一题移动骑士UVA - 439 Knight Moves ,移动方式不一样而已,可以在三维平面上下层移动,坐标移动方式改一下,感觉没太大区别。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int map[50][50][50];
char tmp[50][50][50];
int a, b, c;
int L, R, C;
struct queue {
    int x, y, z;
    int t;
    queue(int a = 0, int b = 0, int c = 0, int d = 0):x(a),y(b),z(c),t(d){}
};
queue q[100000];
int dd[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,-1},{0,0,1}};
int bfs(int x, int y, int z) {
    if (x == a && y == b && z == c) return 0;
    q[0] = queue(x, y, z, 0);

    int t1 = 0, t2 = 1;
    while (t1 < t2) {
        struct queue now = q[t1++];
        for (int i = 0; i < 6; i++) {
            x = now.x + dd[i][0];
            y = now.y + dd[i][1];
            z = now.z + dd[i][2];
            if (x < 0 || x >= L || y < 0 || y >= R || z < 0 || z >= C) continue;
            if (!map[x][y][z]) {
                if (x == a && y == b && z == c) return now.t+1;
                q[t2++] = queue(x, y, z, now.t+1);
                map[x][y][z] = 1;
            }
        }
    }
    return -1;
}
int main() {
    while (scanf("%d%d%d", &L, &R, &C) != EOF && L+R+C) {
        memset(map, 0, sizeof(map));
        int x, y, z;
        for (int i = 0; i < L; i++)
            for (int j = 0; j < R; j++)
                scanf("%s", tmp[i][j]);

        for (int i = 0; i < L; i++)
            for (int j = 0; j < R; j++)
                for (int k = 0; k < C; k++)
                    if (tmp[i][j][k] == 'S') x = i, y = j, z = k;
                    else if (tmp[i][j][k] == 'E') a = i, b = j, c = k;
                    else if (tmp[i][j][k] == '#') map[i][j][k] = 1;

        int ans = bfs(x, y, z);
        if (ans == -1) printf("Trapped!\n");
        else printf("Escaped in %d minute(s).\n", ans);

    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值