其他分享
首页 > 其他分享> > 简单搜索(BFS广搜/DFS)Dungeon Master POJ - 2251 题解

简单搜索(BFS广搜/DFS)Dungeon Master POJ - 2251 题解

作者:互联网

简单搜索[BFS广搜/DFS]Dungeon Master POJ - 2251 题解

题目

Description:

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input:

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.

 example:
 3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Output:

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!

 example:
Escaped in 11 minute(s).
Trapped! 

解析

参考源码

深搜

/***************DFS TLE*****************/
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

char dg[32][32][32];
bool pass[32][32][32];
int L, R, C;
int si, sj, sk;
int level[10] = { 1,-1,0,0,0,0 };
int row[10] = { 0,0,1,-1,0,0 };
int column[10] = { 0,0,0,0,1,-1 };
int finalLen;
bool outSide(int i, int j, int k) {
    if (i > L || i < 1 || j>R || j<1 || k>C || k < 1 || dg[i][j][k] == '#') {
        return true;  // 越界或遇到石头
    }
    return false;
}
void dfs(int nowi, int nowj, int nowk,int length) {
    if (length > finalLen) {
        return;
    }
    if (pass[nowi][nowj][nowk] || outSide(nowi, nowj, nowk)) {
        return;
    }
    if (dg[nowi][nowj][nowk] == 'E') {
        finalLen = min(finalLen, length);
        return;
    }

    pass[nowi][nowj][nowk] = true;  // passby -> true

    for (int i = 0; i < 6; i++) {
        nowi += level[i];
        nowj += row[i];
        nowk += column[i];
        dfs(nowi,nowj,nowk,length+1);
        nowi -= level[i];
        nowj -= row[i];
        nowk -= column[i];
    }

    pass[nowi][nowj][nowk] = false; // tarckback -> false
}
int main()
{
    while (cin >> L >> R >> C && L * R * C != 0) {
        for (int i = 1; i <= L; i++) {
            for (int j = 1; j <= R; j++) {
                for (int k = 1; k <= C; k++) {
                    pass[i][j][k] = false;
                    cin >> dg[i][j][k];
                    if (dg[i][j][k] == 'S') {
                        si = i;
                        sj = j;
                        sk = k;
                    }
                }
            }
        }
        finalLen = 0x3f3f3f3f;
        pass[si][sj][sk] == true;
        dfs(si, sj, sk, 0);
        if (finalLen == 0x3f3f3f3f) {
            cout << "Trapped!" << endl;
        }
        else {
            cout << "Escaped in " << finalLen << " minute(s)." << endl;
        }
    }

    return 0;
}

广搜

/***************BFS AC*****************/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct Point
{
    int x, y, z;            //位置坐标
    int step;                //出发点到该点的步数
}Point;
Point s;
Point nowp;
Point nextp;
char dg[32][32][32];
bool pass[32][32][32];
int L, R, C;
int level[10] = { 1,-1,0,0,0,0 };
int row[10] = { 0,0,1,-1,0,0 };
int column[10] = { 0,0,0,0,1,-1 };
bool check(int i, int j, int k) {
    if (i > L || i < 1 || j>R || j<1 || k>C || k < 1 || dg[i][j][k] == '#' || pass[i][j][k]) {
        return true;  // 越界或遇到石头或已经遍历过
    }
    return false;
}
void bfs()
{
    Point next;
    queue<Point>q;
    q.push(s);
    pass[s.x][s.y][s.z] = true;
    while (!q.empty())
    {
        nowp = q.front();
        q.pop();
        if (dg[nowp.x][nowp.y][nowp.z] == 'E') {
            return;
        }
        else
        {
            for (int i = 0; i < 6; i++)  // 拓展节点
            {
                next.x = nowp.x + level[i];
                next.y = nowp.y + row[i];
                next.z = nowp.z + column[i];
                next.step = nowp.step + 1;
                if (check(next.x, next.y, next.z)) {
                    continue;
                }
                pass[next.x][next.y][next.z] = true;
                q.push(next);
            }
        }
    }
}
int main()
{
    while (cin >> L >> R >> C && L * R * C != 0) {
        for (int i = 1; i <= L; i++) {
            for (int j = 1; j <= R; j++) {
                for (int k = 1; k <= C; k++) {
                    pass[i][j][k] = false;
                    cin >> dg[i][j][k];
                    if (dg[i][j][k] == 'S') {
                        s.x = i;
                        s.y = j;
                        s.z = k;
                        s.step = 0;
                    }
                }
            }
        }

        bfs();
        if (dg[nowp.x][nowp.y][nowp.z] == 'E') {
            cout << "Escaped in " << nowp.step << " minute(s)." << endl;
        }
        else {
            cout << "Trapped!" << endl;
        }
    }

    return 0;
}

标签:Dungeon,int,题解,pass,DFS,next,dg,32,nowp
来源: https://blog.csdn.net/weixin_46360993/article/details/114632596