POJ - 2251 Dungeon Master(搜索)

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!

bfs:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;

struct node
{
    int z,x,y;
    int step;
    node(){};
    node(int z1,int x1,int y1,int step1):z(z1),x(x1),y(y1),step(step1){}
};
int l,n,m,z1,x1,y1,z2,x2,y2,minn;
bool vis[50][50][50];
bool flag;
char mp[50][50][50];
int dir[6][3]={
  {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
node s,e;
void bfs(node s,node e)
{
    flag=0;
    queue<node>q;
    q.push(node(s.z,s.x,s.y,s.step));
    while(!q.empty())
    {
        node a=q.front();
        q.pop();

        for(int i=0;i<6;i++)
        {
            int zz=a.z+dir[i][0];
            int xx=a.x+dir[i][1];
            int yy=a.y+dir[i][2];
            if(zz>=1&&zz<=l&&xx>=1&&xx<=n&&yy>=1&&yy<=m&&!vis[zz][xx][yy]&&mp[zz][xx][yy]=='.')
            {
                q.push(node(zz,xx,yy,(a.step+1)));
                vis[zz][xx][yy]=1;
                if(zz==e.z&&xx==e.x&&yy==e.y)
                {
                    minn=a.step+1;
                    flag=1;
                    return ;
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&l,&n,&m)&&l+n+m)
    {
        memset(vis,0,sizeof(vis));
        getchar();
        for(int i=1;i<=l;i++)
        {
            for(int j=1;j<=n;j++)
            {
                for(int k=1;k<=m;k++)
                {
                    scanf("%c",&mp[i][j][k]);
                    if(mp[i][j][k]=='S')
                    {
                        s.z=i;
                        s.x=j;
                        s.y=k;
                    }
                    if(mp[i][j][k]=='E')
                    {
                        e.z=i;
                        e.x=j;
                        e.y=k;
                        mp[i][j][k]='.';
                    }
                }
                getchar();
            }
            getchar();
        }
        bfs(s,e);
        if(flag)
            cout<<"Escaped in "<<minn<<" minute(s)."<<'\n';
        else
            cout<<"Trapped!"<<'\n';
    }
    return 0;
}

dfs:(过不了 超时)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int N=5e6+30;
int dri[6][3]= {
  {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool vis[50][50][50];
char mp[50][50][50];
int step[50][50][50];
int n,minn,l,r,c;
bool flag;
int x1,y1,z1,x2,y2,z2;

void dfs(int x,int y,int z,int tmp)
{
    if(tmp>=minn||step[x][y][z]<=tmp)
        return ;
    step[x][y][z]=tmp;
    for(int i=0; i<6; i++)
    {
        int xx=x+dri[i][0];
        int yy=y+dri[i][1];
        int zz=z+dri[i][2];
        if(xx<=l&&xx>=1&&yy<=r&&yy>=1&&zz<=c&&zz>=1&&mp[xx][yy][zz]=='.'&&!vis[xx][yy][zz])
        {
            vis[xx][yy][zz]=1;
            if(xx==x2&&yy==y2&&zz==z2)
            {
                flag=1;
                minn=min(minn,tmp+1);
            }
            else
                dfs(xx,yy,zz,tmp+1);
            vis[xx][yy][zz]=0;
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
    {
        memset(vis,0,sizeof(vis));
        memset(step,inf,sizeof(step));
        getchar();
        flag=0;
        for(int i=1; i<=l; i++)
        {
            for(int j=1; j<=r; j++)
            {
                for(int k=1; k<=c; k++)
                {
                    scanf("%c",&mp[i][j][k]);
                    if(mp[i][j][k]=='S')
                    {
                        x1=i;
                        y1=j;
                        z1=k;
                    }
                    if(mp[i][j][k]=='E')
                    {
                        x2=i;
                        y2=j;
                        z2=k;
                        mp[i][j][k]='.';
                    }
                }
                getchar();
            }
            getchar();
        }
        minn=inf;
        dfs(x1,y1,z1,0);
        if(flag)
            cout<<"Escaped in "<<minn<<" minute(s)."<<'\n';
        else
            cout<<"Trapped!"<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
5015次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1103次浏览 27人参与
# 米连集团26产品管培生项目 #
4130次浏览 198人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6907次浏览 37人参与
# 简历第一个项目做什么 #
31251次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186349次浏览 1115人参与
# MiniMax求职进展汇总 #
22892次浏览 293人参与
# 面试紧张时你会有什么表现? #
30371次浏览 188人参与
# 简历中的项目经历要怎么写? #
309379次浏览 4152人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10687次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56698次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160394次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62406次浏览 728人参与
# 你怎么看待AI面试 #
179273次浏览 1164人参与
# 正在春招的你,也参与了去年秋招吗? #
362529次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466318次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务