前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ - 2251 Dungeon Master(搜索)

POJ - 2251 Dungeon Master(搜索)

作者头像
风骨散人Chiam
发布2020-10-28 09:34:16
5250
发布2020-10-28 09:34:16
举报
文章被收录于专栏:CSDN旧文CSDN旧文

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!

这个题目吧,是一个三维的搜索题目,题目不难,但是控制条件的地方一定要处理好,不然会爆内存,问的是最快什么时候逃出去,那么是广搜。

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
const int N=40;
int h,m,n,s,x,y,z;
char  a[N][N][N];
int dl[6][3]={{0,0,-1},{0,0,1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0}};
struct loc
{
    int x,y,z,cnt;
    loc(int x,int y,int z,int cnt):x(x),y(y),z(z),cnt(cnt){}
};
int  pd(int x,int y,int z)
{
    if(x<0||x>h||y<0||y>m||z<0||z>n) return 0;
    else if(a[x][y][z]=='#')return 0;
    else if(a[x][y][z]=='E') return -1;
    else if(a[x][y][z]=='.') return 1;
    else return 0;
}
int main()
{
    queue<loc>dem;
    while(cin>>h>>m>>n){
        if(h==0)break;
        for(int i=0;i<h;i++)
            for(int j=0;j<m;j++)
                for(int k=0;k<n;k++)
                {
                    cin>>a[i][j][k];
                    if(a[i][j][k]=='S')x=i,y=j,z=k;
                }
        while(!dem.empty())dem.pop();
        loc tem(x,y,z,0);
        dem.push(tem);
        while(!dem.empty())
        {
            tem=dem.front();
             a[tem.x][tem.y][tem.z]='#';
            dem.pop();
            for(int i=0;i<6;i++)
            {
                if(pd(tem.x+dl[i][0],tem.y+dl[i][1],tem.z+dl[i][2])==-1)
                {
                    loc t(tem.x+dl[i][0],tem.y+dl[i][1],tem.z+dl[i][2],tem.cnt+1);
                    tem=t;
                    goto en;
                    //cout<<tem.x+dl[i][0]<<' '<<tem.y+dl[i][1]<<' '<<tem.z+dl[i][2]<<endl;
                }
                else if(pd(tem.x+dl[i][0],tem.y+dl[i][1],tem.z+dl[i][2])==1)
                {
                    loc t(tem.x+dl[i][0],tem.y+dl[i][1],tem.z+dl[i][2],tem.cnt+1);
                    dem.push(t);
                    a[t.x][t.y][t.z]='#';
                    //cout<<tem.x+dl[i][0]<<' '<<tem.y+dl[i][1]<<' '<<tem.z+dl[i][2]<<endl;
                }
            }
        }

         cout<<"Trapped!"<<endl;
         continue;
         en: printf("Escaped in %d minute(s).\n",tem.cnt);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档