POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 48380   Accepted: 18252

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

 

跟平常的bfs问题的区别就是方向有6个

复制代码
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define max_v 35
using namespace std;
int dir[6][3]={{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}};//6个方向 东,南,西,北,上,下
char G[max_v][max_v][max_v];
int vis[max_v][max_v][max_v];
int sx,sy,sz,fx,fy,fz;
int n,m,k;
int ans;
struct node
{
    int x,y,z;
    int step;
};
bool check(node a)//检查该点合法性
{
    if(a.x<0||a.x>=n||a.y<0||a.y>=m||a.z<0||a.z>=k)
        return false;
    else if(G[a.z][a.x][a.y]=='#')
        return false;
    else if(vis[a.z][a.x][a.y])
        return false;
    else
        return true;
}
void bfs(int x,int y,int z)
{
    queue<node> q;
    node p,next;

    p.x=x;
    p.y=y;
    p.z=z;
    p.step=0;

    q.push(p);

    while(!q.empty())
    {
        p=q.front();
        q.pop();

        if(p.x==fx&&p.y==fy&&p.z==fz)
        {
            ans=p.step;
            return ;
        }

        for(int i=0;i<6;i++)
        {
            next.x=p.x+dir[i][2];
            next.y=p.y+dir[i][1];
            next.z=p.z+dir[i][0];

            if(check(next))
            {
                vis[next.z][next.x][next.y]=1;
                next.step=p.step+1;
                q.push(next);
            }

        }
    }
}
int main()
{
    while(~scanf("%d %d %d",&k,&n,&m))
    {
        if(n==0&&m==0&&k==0)
            break;

        for(int z=0;z<k;z++)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%s",G[z][i]);
                for(int j=0;j<m;j++)
                {
                    if(G[z][i][j]=='S')
                        sx=i,sy=j,sz=z;
                    else if(G[z][i][j]=='E')
                        fx=i,fy=j,fz=z;
                }
            }
        }

        memset(vis,0,sizeof(vis));
        ans=-1;

        bfs(sx,sy,sz);

        if(ans==-1)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",ans);
    }
    return 0;
}
复制代码

 

 

 

 

posted @   西*风  阅读(309)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示