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

 

全部评论

相关推荐

斑驳不同:还为啥暴躁 假的不骂你骂谁啊
点赞 评论 收藏
分享
蚂蚁 基架java (n+6)*16 签字费若干
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务