UVA 532-Dungeon Master

UVA 532-Dungeon Master

题目大意:给出一个三维图,#代表障碍物,E代表出口,S代表初始位置,求离开的最短路

解题思路:三维数组bfs求最短路

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
struct dun {
    int x, y, z, m;
};
int l, h, w;
int ch[50][50][50];
int dir[6][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1}};
queue<dun> que;
int s;

int bfs() {
    while(!que.empty()) {
        for(int i = 0; i < 6; i++) {
            int a = que.front().z + dir[i][0], b = que.front().x + dir[i][1], c = que.front().y + dir[i][2];
            if(a >= 0 && a < l && b >= 0 && b < h && c >= 0 && c < w){
                if(ch[a][b][c] == 2)
                    return que.front().m + 1;
                else if(ch[a][b][c] == 0) {
                    dun p;
                    p.z = a;
                    p.x = b;
                    p.y = c;
                    p.m = que.front().m + 1;
                    que.push(p);
                    ch[a][b][c] = 1;
                }
            }
        }
        que.pop();
    }
    return -1;
}
int main() {
    while(cin >> l >> h >> w && l + h + w != 0) {
        memset(ch, 0, sizeof(ch));
        while(!que.empty())
            que.pop();
        char str[35];
        getchar();
        for(int i = 0; i < l; i++) {
            for(int j = 0; j < h; j++) {
                gets(str);
                for(int k = 0; k < w; k++) {
                    if(str[k] == 'S'){
                        dun g;
                        g.x = j;
                        g.y = k;
                        g.z = i;
                        g.m = 0;
                        ch[i][j][k] = 1;
                        que.push(g);
                    }
                    else if(str[k] == 'E') {
                        ch[i][j][k] = 2;
                    }
                    else if(str[k] == '#')
                        ch[i][j][k] = 1;
                }
            }
            gets(str);
        }
        s = bfs();
        if(s == -1)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n", s);


    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值