POJ-2251-Dungeon Master

Posted ydddd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-2251-Dungeon Master相关的知识,希望对你有一定的参考价值。

题目大意:

三维迷宫,从‘S‘走到‘E‘,‘.‘可走,‘#‘不可走。

思路:

BFS 六个方向。

代码:

#include <iostream>
#include <string>
#include <queue>
#include <memory.h>
using namespace std;

const int MAXN = 35;
struct Node
{
    int _l,_r,_c;
    int _step;
    Node(int l,int r,int c,int step)
    {
        _l = l;
        _r = r;
        _c = c;
        _step = step;
    }
};
char Map[MAXN][MAXN][MAXN];
int vis[MAXN][MAXN][MAXN];
const int Next[6][3] = {{0,-1,0},{0,0,1},{0,1,0},{0,0,-1},{1,0,0},{-1,0,0}};
int l,r,c;
int start_l,start_r,start_c;
int end_l,end_r,end_c;

int main()
{
    //while (~scanf("%d%d%d",&l,&r,&c)&&l)
    while (cin >> l >> r >> c && l)
    {
        memset(vis,0,sizeof(vis));
        for (int i = 1; i <= l; i++)
            for (int j = 1;j <= r;j++)
                for (int k = 1;k <= c;k++)
                {
                    //scanf("%c", &Map[i][j][k]);
                    cin >> Map[i][j][k];
                    if (Map[i][j][k] == ‘S‘)
                        start_l = i, start_r = j, start_c = k;
                    if (Map[i][j][k] == ‘E‘)
                        end_l = i, end_r = j, end_c = k;
                }
        queue<Node> Q;
        Q.push(Node(start_l,start_r,start_c,0));
        vis[start_l][start_r][start_c] = 1;
        int flag = 0;
        while (!Q.empty())
        {
            Node now = Q.front();
            if (now._l == end_l && now._r == end_r && now._c == end_c)
            {
                flag = 1;
                break;
            }
            for (int i = 0;i<6;i++)
            {
                int next_l = now._l+Next[i][0];
                int next_r = now._r+Next[i][1];
                int next_c = now._c+Next[i][2];
                if (next_l < 1||next_l > l||next_r < 1||next_r > r||next_c < 1||next_c > c)
                    continue;
                if (Map[next_l][next_r][next_c] == ‘#‘||vis[next_l][next_r][next_c] == 1)
                    continue;
                vis[next_l][next_r][next_c] = 1;
                Q.push(Node(next_l,next_r,next_c,now._step+1));
            }
            Q.pop();
        }
        if (flag)
            printf("Escaped in %d minute(s).
",Q.front()._step);
        else
            printf("Trapped!
");

    }

    return 0;
}

  

以上是关于POJ-2251-Dungeon Master的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2251 Dungeon Master

POJ 2251 Dungeon Master

POJ 2251 Dungeon Master (BFS)

POJ-2251 Dungeon Master

POJ2251 Dungeon Master

POJ 2251 Dungeon Master(BFS)