欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Dungeon Master POJ - 2251 (搜索)

程序员文章站 2022-12-24 08:33:02
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48605 Accepted: 18339 Description You are trapped in a 3D dungeon and need t ......
dungeon master
time limit: 1000ms   memory limit: 65536k
total submissions: 48605   accepted: 18339

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. 

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

sample output

escaped in 11 minute(s).
trapped!

source

 

题意:给你一个多维的迷宫,问能不能可以从起点走到终点,可以上下穿梭层数,也可以东南西北走,都是一秒一个单位,可以到终点就输出步数,不可以就输出那句话

思路:其实和普通的二维迷宫差不多,就是多了一个层数,也就是多了两个方向(上下层数)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-10
#define pi acos(-1.0)
#define ll long long
int const maxn = 35;
const int mod = 1e9 + 7;
int gcd(int a, int b) {
    if (b == 0) return a;  return gcd(b, a % b);
}

char map[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int k,n,m,sx,sy,sz,ex,ey,ez;
int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};

struct node
{
    int x,y,z,step;
};
int check (int x,int y,int z)
{
    if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m)
        return 1;
    else if(map[x][y][z]=='#')
        return 1;
    else if(vis[x][y][z])
        return 1;
    return 0;
}

int bfs()
{
    node a,next;
    queue<node>que;
    a.x=sx;  a.y=sy;  a.z=sz;
    a.step=0;
    vis[sx][sy][sz]=1;
    que.push(a);
    while(!que.empty())
    {
        a=que.front();
        que.pop();
        if(a.x == ex && a.y == ey && a.z == ez)
            return a.step;
        for(int i=0;i<6;i++)
        {
            next=a;
            next.x=a.x+dir[i][0];
            next.y=a.y+dir[i][1];
            next.z=a.z+dir[i][2];
            if(check(next.x,next.y,next.z))
                continue;
            vis[next.x][next.y][next.z]=1;
            next.step=a.step+1;
            que.push(next);

        }
    }
    return 0;

}


int main()
{
    while(~scanf("%d %d %d",&k,&n,&m))
    {
        if(k==0 && n==0 && m==0)
            break;
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%s",map[i][j]);
                for(int r=0;r<m;r++)
                {
                    if(map[i][j][r]=='s')
                    {
                        sx=i;sy=j;sz=r;
                    }
                    else if(map[i][j][r]=='e')
                    {
                        ex=i;ey=j;ez=r;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));
        int ans;
        ans=bfs();
        if(ans)
            printf("escaped in %d minute(s).\n",ans);
        else
            printf("trapped!\n");
    }
    return 0;
}