POJ - 2251 Dungeon Master

  这是一道简单的搜索题,题意是给你一个立体图形L,R,C。其中L代表高,R和C代表长和宽,从S出发到E,中间只能走'.',不能走'#',一次只能向上或者向下或者向前向后向左向右一步。和正常的二维的BFS相同,只不过加了一个维度,但是具体操作没有改变,还是求两点间的最短距离,加上向上向下两种情况即可。

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>

using namespace std;

int k, n, m, ans;
int ax[6] = {0, 0, 1, -1, 0, 0},
    ay[6] = {0, 0, 0, 0, 1, -1},
    az[6] = {-1, 1, 0, 0, 0, 0};
char M[50][50][50];
int used[50][50][50];

struct l{
    int x, y, z;
    int step;
}a, b;

bool ff(int z, int x, int y)
{
    if(z >= 0 && z < k && x >= 0 && x < n && y >= 0 && y < m)
        return true;
    return false;
}

void BFS()
{
    queue<l> q;
    l f, s;
    q.push(a);
    while(!q.empty()) {
        f = q.front();
        q.pop();
        for(int i = 0; i < 6; i++) {
            s.z = f.z + az[i];
            s.x = f.x + ax[i];
            s.y = f.y + ay[i];
            s.step = f.step + 1;
            if(ff(s.z, s.x, s.y) && !used[s.z][s.x][s.y] && M[s.z][s.x][s.y] != '#') {
                used[s.z][s.x][s.y] = 1;
                if(M[s.z][s.x][s.y] == 'E') {
                    ans = s.step;
                    return;
                }
                q.push(s);
            }
        }
    }
}

int main()
{
    while(cin >> k >> n >> m && k) {
        memset(used, 0, sizeof(used));
        ans = 0;
        for(int t = 0; t < k; t++) {
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    cin >> M[t][i][j];
                    if(M[t][i][j] == 'S') {
                        a.z = t;
                        a.x = i;
                        a.y = j;
                        a.step = 0;
                    }
                    if(M[t][i][j] == 'E') {
                        b.z = t;
                        b.x = i;
                        b.y = j;
                    }
                }
            }
        }
        BFS();
        if(ans) {
            cout << "Escaped in "<< ans << " minute(s)." << endl;
        }
        else
            cout << "Trapped!" << endl;
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值