UVA532 Dungeon Master

难度:4
知识点:广度优先搜索

这道题算是比较简单的了,相对于它的难度分级来说,就是一个三维状态空间的无权图最短路,用bfs跑一边就行了,算是一个比较基础,来检验你bfs基本功底的题目,

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) x.size())
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

const int N = 35;

struct node {
    int x, y, l;
    node(int a, int b, int c): x(a), y(b), l(c) {}
};

int level, n, m, sx, sy, sl, ex, ey, el;
int dx[6] = {0, -1, 0, 1, 0, 0};
int dy[6] = {0, 0, 1, 0, -1, 0};
int dl[6] = {1, 0, 0, 0, 0, -1};
string s[N][N];

void bfs() {
    queue<node> q;
    q.push(node(sx, sy, sl));
    int dist[N][N][N];
    fill((int*) dist, (int*) dist + N * N * N, -1);
    dist[sx][sy][sl] = 0;
    while (!q.empty()) {
        node now = q.front(); q.pop();
        if (now.x == ex && now.y == ey && now.l == el) { 
            cout << "Escaped in " << dist[ex][ey][el] << " minute(s)." << endl; return;
        }
        for (int i = 0; i < 6; i++) {
            int x = now.x + dx[i], y = now.y + dy[i], l = now.l + dl[i];
            if (x < 0 || y < 0 || l < 0 || x >= n || y >= m || l >= level) continue;
            if (dist[x][y][l] > -1 || s[l][x][y] == '#') continue;
            q.push(node(x, y, l)); dist[x][y][l] = dist[now.x][now.y][now.l] + 1;
        }
    }
    cout << "Trapped!" << endl;
}

int main() {
    while (cin >> level >> n >> m && level) {
        for (int i = 0; i < level; i++) {
            for (int j = 0; j < n; j++) {
                cin >> s[i][j];
                for (int k = 0; k < m; k++) {
                    if (s[i][j][k] == 'S') { sx = j; sy = k; sl = i; s[i][j][k] = '.'; }
                    if (s[i][j][k] == 'E') { ex = j; ey = k; el = i; s[i][j][k] = '.'; }
                }
            }
        }
        bfs();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值