POJ2251—— BFS——Dungeon Master

http://poj.org/problem?id=2251

/*
简单bfs,向六个方向拓展

*/
/************************************************
* Author        :Powatr
* Created Time  :2015-8-9 9:23:15
* File Name     :B.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dirx[] = {1, -1, 0, 0, 0};
int diry[] = {0, 0, 1, -1, 0};
struct P{
    int z, x, y;
    int step;
}a[110];
int L, R, C;
int sx, sy, sz, ex, ey, ez;
queue <P> q;
bool vis[40][40][40];
char maze[40][40][40];
bool ok(int z, int x, int y)
{
    if(x >= 1 && x <= R && y >= 1 && y <= C && z >= 1 && z <= L && (maze[z][x][y] == ‘.‘ || maze[z][x][y] == ‘S‘ || maze[z][x][y] == ‘E‘) && !vis[z][x][y] )
        return true;
    return false;
}
int main(){
    while(~scanf("%d%d%d", &L, &R, &C) && L && R && C){
        getchar();
        memset(vis, false, sizeof(vis));
        memset(maze, ‘.‘, sizeof(maze));
        for(int i = 1; i <= L; i++){
            for(int j = 1; j <= R; j++){
                for(int k = 1; k <= C; k++){
                    scanf("%c", &maze[i][j][k]);
                    if(maze[i][j][k] == ‘S‘){
                        sx = j, sy = k, sz = i;
                    }
                    if(maze[i][j][k] == ‘E‘){
                        ex = j, ey = k, ez = i;
                    }
                }
                getchar();
            }
            getchar();
        }
        while(!q.empty())
        q.pop();
        q.push((P){sz, sx, sy, 0});
        int flag = 1;
        while(!q.empty()){
            P now = q.front();
            q.pop();
            int x = now.x, y = now.y, z = now.z;
            int step = now.step;
            if(!ok(z, x, y)) continue;
            if(x == ex && y == ey && z == ez) {
                printf("Escaped in %d minute(s).\n", step);
                flag = 0;
                break;
            }
            for(int i = 0; i < 4; i++){
               int dx = x + dirx[i], dy = y + diry[i];
         //   printf("%d %d %d\n", z, dx, dy);
                if(ok(z, dx, dy)){
                    vis[z][x][y] = true;
                    q.push((P){z, dx, dy, step+1});
                }
            }
            if(ok(z+1, x, y)){
                vis[z][x][y] = true;
                q.push((P){z+1, x, y, step+1});
            }
            if(ok(z-1, x, y)){
                vis[z][x][y] = true;
                q.push((P){z-1, x, y, step+1});
            }
        }
        if(flag) printf("Trapped!\n");
    }
    return 0;
}

  

时间: 2024-10-09 05:41:25

POJ2251—— BFS——Dungeon Master的相关文章

【POJ2251】Dungeon Master

本题传送门 本题知识点:宽度优先搜索 题意简单.在一个L层高的楼里,去走迷宫,就是问从S走到E的最短路径.每走一格每上或者下一层都算1步. 一开始以为这个"立体迷宫"有点吓到我(题做得太少了),后来发觉,只是一个三维数组以及多了两个操作方向(原地向上或者原地向下),除此之外就是简单的bfs模板题了. 数据很小. // POJ 2251 #include<iostream> #include<cstdio> #include<cstring> #inc

poj2251——bfs

POJ 2251  bfs Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18245   Accepted: 7075 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

hdu 2251 Dungeon Master bfs

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 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(用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: 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

ZOJ 1940 Dungeon Master 三维BFS

Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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 r

poj2251:Dungeon Master

最初没有注意到结果是要求最小的步数,那么就成了最基本的迷宫找到一条出路的问题并记下找到出路时,所花的步数,那么很容易得到代码如下: 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 #define MAX 1000000 7 int l,r,c,coun; 8 char m[30][30][30]; 9 bool flag[30][30]

POJ 2251 Dungeon Master(bfs)

Dungeon Master 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 dow

POJ:Dungeon Master(三维bfs模板题)

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