POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

  POJ 2251

  题目大意: 给出一三维空间的地牢,要求求出由字符‘S‘到字符‘E‘的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间。不同L层的地图,相同RC坐标处是相连通的。(.可走,#为墙)

  解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住。

/* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int n, m, o; //n行m列,o为层
char mapp[31][31][31];
bool visit[31][31][31]; //标记访问状态

/* 构造队列元素 (点位置+步数)*/
struct Point{
    int x, y, z;
    int step;
    Point(){}
    Point(int a, int b, int c, int d) :x(a), y(b), z(c), step(d){}
};

/* 判断点是否满足访问条件 */
inline bool judge(Point tmp){
    if (mapp[tmp.x][tmp.y][tmp.z] == ‘#‘ || visit[tmp.x][tmp.y][tmp.z]
        || tmp.x < 0 || tmp.x >= o || tmp.y < 0 || tmp.y >= n
        || tmp.z < 0 || tmp.z >= m
        ){
        return 0;
    }
    return 1;
}

/* x0为深度 y0,z0为同一层坐标 */
void bfs(int x0, int y0,int z0){
    queue<Point> q;
    bool found = 0;
    visit[x0][y0][z0] = 1;  //起点标记已访问
    q.push(Point(x0, y0, z0, 0));
    while (!q.empty()){
        Point tmp = q.front(); q.pop();
        if (mapp[tmp.x][tmp.y][tmp.z] == ‘E‘){
            //找到终点,搜索结束,输出最短路
            printf("Escaped in %d minute(s).\n", tmp.step);
            found = 1;
            break;
        }
        else{
            Point tmp2;
            ++tmp.step; //步数必定增加

            //往上一层
            tmp2 = tmp;
            tmp2.x = tmp.x - 1;
            if (judge(tmp2)){
                visit[tmp2.x][tmp2.y][tmp2.z] = 1;
                q.push(tmp2);
            }

            //往下一层
            tmp2 = tmp;
            tmp2.x = tmp.x + 1;
            if (judge(tmp2)){
                visit[tmp2.x][tmp2.y][tmp2.z] = 1;
                q.push(tmp2);
            }

            //以下四种为同一层

            //往上一行
            tmp2 = tmp;
            tmp2.y = tmp.y - 1;
            if (judge(tmp2)){
                visit[tmp2.x][tmp2.y][tmp2.z] = 1;
                q.push(tmp2);
            }

            //往下一行
            tmp2 = tmp;
            tmp2.y = tmp.y + 1;
            if (judge(tmp2)){
                visit[tmp2.x][tmp2.y][tmp2.z] = 1;
                q.push(tmp2);
            }

            //往左一列
            tmp2 = tmp;
            tmp2.z = tmp.z - 1;
            if (judge(tmp2)){
                visit[tmp2.x][tmp2.y][tmp2.z] = 1;
                q.push(tmp2);
            }

            //往右一列
            tmp2 = tmp;
            tmp2.z = tmp.z + 1;
            if (judge(tmp2)){
                visit[tmp2.x][tmp2.y][tmp2.z] = 1;
                q.push(tmp2);
            }

        }//else
    }//while
    if (!found){
        printf("Trapped!\n");
    }
}

int main()
{
#ifdef _LOCAL
    freopen("D:\\input.txt", "r", stdin);
#endif

    int sx, sy, sz;
    while (scanf("%d%d%d", &o, &n, &m) == 3 && (m + n + o)){
        memset(visit, 0, sizeof visit);
        for (int i = 0; i < o; ++i){
            for (int j = 0; j < n; ++j){
                scanf("%s", mapp[i][j]);
                for (int k = 0; k < m; ++k){
                    if (mapp[i][j][k] == ‘S‘){
                        sx = i;
                        sy = j;
                        sz = k;
                    }
                }//for(k)
            }//for(j) n行
        }//for(i) o层
        bfs(sx, sy, sz); //sx是深度
    }

    return 0;
}

时间: 2024-10-12 17:40:47

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)的相关文章

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 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 wit

poj 2251 Dungeon Master(三维BFS)(中等)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20598   Accepted: 7971 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

BFS POJ 2251 Dungeon Master

题目传送门 1 /* 2 BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <queue> 8 using namespace std; 9 10 const int MAXN = 33; 11 const int INF = 0x3f3

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ - 2251 Dungeon Master(三维BFS)

题目链接:http://poj.org/problem?id=2251 题意:三维BFS. 题解:大水题,只不过多加了两个方向 1 //poj2251 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int sx,sy,sz,ex,ey,ez,L,R,C; 8 const int INF=

POJ 2251 Dungeon Master【三维BFS模板】

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 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 wi

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 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

POJ - 2251 - Dungeon Master (简单BFS)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20450   Accepted: 7917 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