简单搜索poj2251 3D迷宫(三维迷宫)

# 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.

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.
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!
sample input
3 4 5
S…
.###.
.##…
###.#

##.##
##…

#.###
####E

1 3 3
S##
#E#

0 0 0
simple output
Escaped in 11 minute(s).
Trapped!

#题目大意

你被困在一个3D地牢里,需要找到最快的出路!地牢是由单位立方体组成的,可以用岩石填充,也可以不用岩石填充。一个单位向北、向南、向东、向西、向上或向下移动需要一分钟。你不能斜行,迷宫四周都是坚固的岩石。有可能逃跑吗?如果是,需要多长时间?

输入

输入由若干地牢组成。每个地牢描述都以一行开头,包含三个整数l、r和c(大小都限制为30)。

l是构成地牢的等级数。

r和c是构成每个级别计划的行数和列数。

接下来是L块R行,每个R行包含C字符。每个角色描述了地牢的一个单元。岩石用’#'表示, ‘.’表示可行路,入口用“S”表示;出口用字母“E”表示。每层后面都有一个空白行。L、R和C的输入以三个零结束。

题意很清楚,思路清晰,就是一道简单而又经典的bfs迷宫问题,只要用三维数组来存图就OK了
献上孱弱 的代码

#include
#include
#include
#include
#define maxn 35
using namespace std;
char pic[maxn][maxn][maxn];//存图
int flag[maxn][maxn][maxn];//标记数组
int ex, ey, ez, succes;
int l, h, w;
int mm[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};//6个行走方向(前、后、左、右、上、下)
typedef struct Trip
{
    int x, y, z, step; // 表示xyz方向以及步数;
}trip;
void bfs(int p, int r, int s)//分别表示xyz
{
    queue<trip>q;//用队列来实现bfs
    trip a, next;//两个结构体a表示当前信息,next表示下一步信息。
    a.x = p;
    a.y = r;
    a.z = s;
    a.step = 0;
    q.push(a);
    flag[p][r][s] = 1;
    while(!q.empty())
    {
        a = q.front();
        q.pop();
        if(a.x == ex && a.y == ey && a.z == ez)
        {
            printf("Escaped in %d minute(s).\n", a.step);
            succes = 1;
            break;
        }
        for(int i = 0; i < 6; i ++)//6步行走
        {
            next.x = a.x + mm[i][0];
            next.y = a.y + mm[i][1];
            next.z = a.z + mm[i][2];
            if(next.x < 0 || next.x >= l || next.y < 0 || next.y >= h || next.z < 0 || next.z >= w || pic[next.x][next.y][next.z] == '#' || flag[next.x][next.y][next.z] == 1 )
            continue;
            next.step = a.step + 1;
            flag[next.x][next.y][next.z] = 1;//标记已经走过的路;
            q.push(next);

        }
    }
}
int main()
{
    while(~scanf("%d%d%d", &l, &h, &w))
    {
        succes = 0;
        if(l == 0 && h == 0 && w == 0)
            break;
        memset(flag, 0, sizeof(flag));
        for(int i = 0; i < l; i++)
        {
            for(int j = 0 ; j < h; j++)
            {
                scanf("%s", pic[i][j]);
            }
        }
        for(int i = 0; i < l; i++)
        {
            for(int j = 0; j < h; j++)
            {
                for(int k = 0; k < w; k++)
                {
                    if(pic[i][j][k] == 'E')//遍历图寻找出口
                    {
                        ex = i;
                        ey = j;
                        ez = k;
                        break;
                    }
                }
            }
        }
        for(int i = 0; i < l; i++)
        {
            for(int j = 0; j < h; j++)
            {
                for(int k = 0; k < w; k++)
                {
                    if(pic[i][j][k] == 'S')//遍历图寻找入口
                        bfs(i, j, k);
                }
            }
        }
        if(succes == 0)//没有找到出口
            printf("Trapped!\n");
    }
}

#原创转载请注明出处

你可能感兴趣的:(搜索,搜索)