【POJ2251】Dungeon Master

本题传送门

本题知识点:宽度优先搜索

题意简单。在一个L层高的楼里,去走迷宫,就是问从S走到E的最短路径。每走一格每上或者下一层都算1步。

一开始以为这个“立体迷宫”有点吓到我(题做得太少了),后来发觉,只是一个三维数组以及多了两个操作方向(原地向上或者原地向下),除此之外就是简单的bfs模板题了。

数据很小。

// POJ 2251
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

// R == H, C == W
int L, R, C;
char maze[35][35][35];
int len[35][35][35];
bool stay[35][35][35];
int bl, br, bc, el, er, ec;
int rl[] = { 0, 0, 0, 0, 1, -1 };
int rc[] = { 1, -1, 0, 0, 0, 0 };
int rr[] = { 0, 0, 1, -1, 0, 0 };

struct node{
    int l, r, c;
};
queue<node> que;

void build(){
    for(int i = 0; i < L; i++){
            for(int j = 0; j < R; j++){
                scanf("%s", maze[i][j]);
                // 找起始点
                for(int k = 0; k < C; k++){
                    if(maze[i][j][k] == 'S'){
                        bl = i; br = j; bc = k;
                    }
                    if(maze[i][j][k] == 'E'){
                        el = i; er = j; ec = k;
                    }
                }
            }
        }
    // 注意队列一定要清空!
    while(!que.empty()) que.pop();
}

void show(){
    cout << "len\n";
    for(int i = 0; i < L; i++){
        for(int j = 0; j < R; j++){
            for(int k = 0; k < C; k++){
                printf("%d ", len[i][j][k]);
            } cout << endl;
        } cout << endl;
    } cout << endl;
}

void bfs(){
    node a;
    a.l = bl; a.r = br; a.c = bc;
    len[bl][br][bc] = 0;
    stay[bl][br][bc] = true;
    que.push(a);


    while(!que.empty()){
        node now = que.front(), next; que.pop();

        if(now.l == el && now.r == er && now.c == ec){
            break;
        }

        // 这里比较写的有点花
        for(int i = 0; i < 6; i++){
            next.l = now.l + rl[i]; next.r = now.r + rr[i]; next.c = now.c + rc[i];
            if(0 <= next.l && next.l < L && 0 <= next.r && next.r < R && 0 <= next.c && next.c < C
               && maze[next.l][next.r][next.c] != '#' && !stay[next.l][next.r][next.c]){
                    stay[next.l][next.r][next.c] = true;
                    len[next.l][next.r][next.c] = len[now.l][now.r][now.c] + 1;
                    que.push(next);
               }
        }
    }
}

int main()
{
    while(scanf("%d %d %d", &L, &R, &C) && L + R + C != 0){
        memset(stay, false, sizeof(stay));
        memset(len, -1, sizeof(len));
        build();

        // bfs
        bfs();

//        show();
        if(len[el][er][ec] != -1)
            printf("Escaped in %d minute(s).\n", len[el][er][ec]);
        else printf("Trapped!\n");
    }
    return 0;
}

//3 3 3
//S.#
//###
//###
//
//#.#
//###
//###
//
//#E#
//###
//###

转载于:https://www.cnblogs.com/Ayanowww/p/11553190.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值