三维BFS poj2251 Dungeon Master

其实跟普通的BFS并没有很大区别,只不过变成了三维而已,,多了向上和向下两个方向


#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 50 + 5;
const int INF = 0x3f3f3f3f;

int H, M, N;
char S[MX][MX][MX];
bool vis[MX][MX][MX];
int dist[][3] = {{1, 0, 0}, { -1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};

struct Point {
    int x, y, h, t;
    Point(int a, int b, int c, int d = 0) {
        x = a;
        y = b;
        h = c;
        t = d;
    }
} FST(0, 0, 0);

int main() {
    //freopen("input.txt","r",stdin);
    while(~scanf("%d%d%d", &H, &M, &N), H + M + N) {
        memset(vis, 0, sizeof(vis));

        bool sign = false;
        for(int i = 1; i <= H; i++) {
            for(int x = 1; x <= M; x++) {
                scanf("%s", S[i][x] + 1);
                for(int y = 1; y <= N; y++) {
                    if(S[i][x][y] == 'S') FST = Point(x, y, i), sign = true;
                }
            }
        }

        if(!sign) {
            printf("Trapped!\n");
            continue;
        }

        queue<Point>work;
        work.push(FST);

        int ans = -1;
        while(!work.empty()) {
            Point fp = work.front();
            work.pop();

            vis[fp.x][fp.y][fp.h] = 1;
            if(S[fp.h][fp.x][fp.y] == 'E') {
                ans = fp.t;
                break;
            }

            for(int k = 0; k < 6; k++) {
                int nx = fp.x + dist[k][0];
                int ny = fp.y + dist[k][1];
                int nh = fp.h + dist[k][2];

                if(nx < 1 || nx > M || ny < 1 || ny > N || nh < 1 || nh > H) continue;
                if(S[nh][nx][ny] == '#' || vis[nx][ny][nh]) continue;

                vis[nx][ny][nh] = 1;
                work.push(Point(nx, ny, nh, fp.t + 1));
            }
        }

        if(ans == -1) {
            printf("Trapped!\n");
        } else {
            printf("Escaped in %d minute(s).\n", ans);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值