poj 2251 Dungeon Master(bfs)

题目链接  http://poj.org/problem?id=2251

题意:一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped!

简单的三维bfs随便做一下就可以了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
char map[40][40][40];
int dr[4][2] = {1 , 0 , 0 , 1 , -1 , 0 , 0 , -1} , vis[40][40][40] , ans , l , r , c;
struct TnT {
    int h , x , y , step;
};
int bfs(int h , int x , int y , int step) {
    memset(vis , 0 , sizeof(vis));
    queue<TnT>q;
    TnT p , gg , gl;
    p.h = h , p.x = x , p.y = y , p.step = step;
    vis[p.h][p.x][p.y] = 1;
    q.push(p);
    while(!q.empty()) {
        gg = q.front();
        if(map[gg.h][gg.x][gg.y] == ‘E‘) {
            return gg.step;
        }
        for(int j = 0 ; j < 3 ; j++) {
            if(j == 0) {
                for(int i = 0 ; i < 4 ; i++) {
                    gl = gg;
                    gl.x += dr[i][0];
                    gl.y += dr[i][1];
                    if(gl.x >= 0 && gl.x < r && gl.y >= 0 && gl.y < c && map[gl.h][gl.x][gl.y] != ‘#‘ && !vis[gl.h][gl.x][gl.y]) {
                        gl.step++;
                        vis[gl.h][gl.x][gl.y] = 1;
                        q.push(gl);
                    }
                }
            }
            if(j == 1) {
                if(gg.h > 0 && map[gg.h - 1][gg.x][gg.y] != ‘#‘ && !vis[gg.h - 1][gg.x][gg.y]) {
                    gl = gg;
                    gl.step++;
                    gl.h--;
                    vis[gl.h][gl.x][gl.y] = 1;
                    q.push(gl);
                }
            }
            if(j == 2) {
                if(gg.h < l - 1 && map[gg.h + 1][gg.x][gg.y] != ‘#‘ && !vis[gg.h + 1][gg.x][gg.y]) {
                    gl = gg;
                    gl.step++;
                    gl.h++;
                    vis[gl.h][gl.x][gl.y] = 1;
                    q.push(gl);
                }
            }
        }
        q.pop();
    }
    return -1;
}
int main()
{
    while(scanf("%d%d%d" , &l , &r , &c)) {
        if(l == 0 || r == 0 || c == 0)
            break;
        int star = 0 , end = 0;
        for(int i = 0 ; i < l ; i++) {
            for(int j = 0 ; j < r ; j++) {
                scanf("%s" , map[i][j]);
                for(int k = 0 ; k < c ; k++) {
                    if(map[i][j][k] == ‘S‘) {
                        star = i;
                    }
                    if(map[i][j][k] == ‘E‘) {
                        end = i;
                    }
                }
            }
        }
        ans = 0;
        for(int i = 0 ; i < r ; i++) {
            for(int j = 0 ; j < c ; j++) {
                if(map[star][i][j] == ‘S‘) {
                    ans = bfs(star , i , j , 0);
                }
            }
        }
        if(ans == -1) {
            printf("Trapped!\n");
        }
        else {
            printf("Escaped in %d minute(s).\n" , ans);
        }
    }
    return 0;
}
时间: 2024-08-23 05:39:01

poj 2251 Dungeon Master(bfs)的相关文章

POJ 2251 Dungeon Master (bfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; char mat[50][50][50]; int vis[50][50][50]; int op[6][3]={0,-1,0, 0,1,0, 1,0,0, -1,0,0 ,0,0,1, 0,0,-1 }; int ok; in

POJ 2251 Dungeon Master(dfs)

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 m

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(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

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)

题目链接: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: 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

POJ 2251:Dungeon Master【bfs】

Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 9   Accepted Submission(s) : 7 Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge