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!
题意:
相当于一栋大楼里面很多秘密通道,S是起始位置,E是终点位置,‘#’是墙,‘.’是路,问从S出发最少经过多长时间就到达E处;

分析:
和迷宫不同的是,迷宫是平面上东南西北的移动,相当于在大楼里面的一层楼里找出口,而这个题目在迷宫的基础上又增加了上下的移动,即大楼里面的上下层之间的移动,
所以需要建立三维的数组,找到S的位置,移动方向由4个增加到6个,直到找到E为止,如果找遍了所有的能走的地方都没找到出口E,就出不来了!!!

C++版本一

BFS

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
int l,r,c,ans;
int sx,sy,sz,ex,ey,ez;
char Map[33][33][33];
int vis[33][33][33];
int dir[6][3]={{1,0,0},
                {-1,0,0},
                {0,0,1},
                {0,0,-1},
                {0,1,0},
                {0,-1,0}};
struct node{
    int x,y,z;
    int step;

}front1,start,temp,end1;
int bfs(){
    queue <node>Q;
    memset(vis,0,sizeof(vis));
    while(!Q.empty())
        Q.pop();
        start.x=sx;
        start.y=sy;
        start.z=sz;
        start.step=0;
        vis[start.x][start.y][start.z]=1;
    Q.push(start);
    while(!Q.empty()){
            front1=Q.front();
            Q.pop();

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

                if(temp.x<0||temp.x>=l||temp.y<0||temp.y>=r||temp.z<0||temp.z>=c||Map[temp.x][temp.y][temp.z]=='#')  continue;

                if(vis[temp.x][temp.y][temp.z]==0){
                    //cout << front1.x<<front1.y<<front1.z <<front1.step<< endl;
                    vis[temp.x][temp.y][temp.z]=1;

                    temp.step=front1.step+1;
                    //cout << temp.x<<temp.y<<temp.z <<temp.step<<"-+-+"<< endl;
                    if(temp.x==ex&&temp.y==ey&&temp.z==ez) return temp.step;
                    Q.push(temp);
                }
            }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d%d",&l,&r,&c)!=EOF){
    if(l==0&&r==0&&c==0)    break;
    for(int i=0;i<l;i++){
        for(int j=0;j<r;j++){
            scanf("%s",Map[i][j]);
            for(int k=0;k<c;k++){
                char temp=Map[i][j][k];
                if(temp=='S'){
                    sx=i;
                    sy=j;
                    sz=k;
                }
                if(temp=='E'){
                    ex=i;
                    ey=j;
                    ez=k;
                }
            }
        }
    }
    ans=bfs();
    if(ans>=0)  cout << "Escaped in "<<ans<<" minute(s)." << endl;
    else {cout << "Trapped!" << endl;}


    }

    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

BFS

//Memory Time 
// 784K  32MS 
 
#include<iostream>
using namespace std;
 
typedef class
{
	public:
		int l,r,c;
		int depth;  //树深(分钟)
}SE;
 
SE s,e;
bool maze[40][40][40];
int shortminute;
 
bool BFS(int k,int i,int j)
{
	bool vist[40][40][40]={false};
 
	SE queue[30000];
	int head,tail;
	queue[head=0].l=k;
	queue[tail=0].r=i;
	queue[0].c=j;
	queue[tail++].depth=1;
 
	vist[k][i][j]=true;
 
	while(head<tail)
	{
		SE x=queue[head++];
 
		if(x.l==e.l && x.r==e.r && x.c==e.c)
		{
			shortminute=x.depth;
			return true;
		}
 
		if(maze[x.l][x.r][x.c-1] && !vist[x.l][x.r][x.c-1])  //West
		{
			vist[x.l][x.r][x.c-1]=true;
			queue[tail].l=x.l;
			queue[tail].r=x.r;
			queue[tail].c=x.c-1;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l][x.r-1][x.c] && !vist[x.l][x.r-1][x.c])  //North
		{
			vist[x.l][x.r-1][x.c]=true;
			queue[tail].l=x.l;
			queue[tail].r=x.r-1;
			queue[tail].c=x.c;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l][x.r][x.c+1] && !vist[x.l][x.r][x.c+1])  //East
		{
			vist[x.l][x.r][x.c+1]=true;
			queue[tail].l=x.l;
			queue[tail].r=x.r;
			queue[tail].c=x.c+1;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l][x.r+1][x.c] && !vist[x.l][x.r+1][x.c])  //South
		{
			vist[x.l][x.r+1][x.c]=true;
			queue[tail].l=x.l;
			queue[tail].r=x.r+1;
			queue[tail].c=x.c;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l-1][x.r][x.c] && !vist[x.l-1][x.r][x.c])  //Up
		{
			vist[x.l-1][x.r][x.c]=true;
			queue[tail].l=x.l-1;
			queue[tail].r=x.r;
			queue[tail].c=x.c;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l+1][x.r][x.c] && !vist[x.l+1][x.r][x.c])  //Down
		{
			vist[x.l+1][x.r][x.c]=true;
			queue[tail].l=x.l+1;
			queue[tail].r=x.r;
			queue[tail].c=x.c;
			queue[tail++].depth=x.depth+1;
		}
	}
	return false;
}
 
int main(int i,int j,int k)
{
	int L,R,C;
	while(cin>>L>>R>>C)
	{
		if(!L && !R && !C)
			break;
 
		/*Initial*/
 
		memset(maze,false,sizeof(maze));
		
		/*Structure the Maze*/
 
		for(k=1;k<=L;k++)
			for(i=1;i<=R;i++)
				for(j=1;j<=C;j++)
				{
					char temp;
					cin>>temp;
					if(temp=='.')
						maze[k][i][j]=true;
					if(temp=='S')
					{
						maze[k][i][j]=true;
						s.l=k;
						s.r=i;
						s.c=j;
					}
					if(temp=='E')
					{
						maze[k][i][j]=true;
						e.l=k;
						e.r=i;
						e.c=j;
					}
				}
 
		/*Search the min Minute*/
 
		if(BFS(s.l,s.r,s.c))
			cout<<"Escaped in "<<shortminute-1<<" minute(s)."<<endl;
		else
			cout<<"Trapped!"<<endl;
 
	}
	return 0;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 14:11
很喜欢小米的新车,校招薪资每月22k,攒多久能买?
测试糕手手:别看工资,先看现金流存款。有50W存款以上再考虑,车是消耗品,选适合自己的重要。你有钱就当我没说过
点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务