Dungeon Master POJ - 2251 (搜索)

2018-09-01 05:38:04来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

Ulm Local 1997
 

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

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

#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;
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:宽字节UTF-8、多字节互转

下一篇:产生随机数与相应的猜拳小游戏