Dungeon Master (BFS)

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!

题意:给定一个三维迷宫,判断能否出去,若能还需记录步数。这道题目和二维的《迷宫问题》很是相似。

附上AC代码:

#include<cstring> 
#include<iostream>
#include<queue>
using namespace std;
struct Node
{
	int x,y,z;
}pre[40][40][40];
int L,R,C,sl,sr,sc,gl,gr,gc,fag;
int mp[40][40][40];
int fl[40][40][40];
int dx[6]={-1,1, 0,0, 0,0};
int dy[6]={ 0,0,-1,1, 0,0};
int dz[6]={ 0,0, 0,0,-1,1};
void bfs()
{
	queue<Node> q;
	Node tmp,next;
	tmp.x=gl;
	tmp.y=gr;
	tmp.z=gc;
	q.push(tmp);
	while (!q.empty())
	{
		tmp=q.front();
		q.pop();//先把队首推出栈,这个之前一直WA,直到同学提醒我 
		if(tmp.x==sl&&tmp.y==sr&&tmp.z==sc)
		{
			fag=1; break;
		}
		//出栈要在判断前 
		int xx,yy,zz;
		for(int i=0;i<6;i++)
		{
			xx=tmp.x+dx[i];
			yy=tmp.y+dy[i];
			zz=tmp.z+dz[i];
			if(xx<0||xx>=L||yy<0||yy>=R||zz<0||zz>=C)  continue;
			//cout<<yy<<zz<<endl;
			if(!mp[xx][yy][zz]&&!fl[xx][yy][zz])
			{
				next.x=xx;
				next.y=yy;
				next.z=zz;
				pre[xx][yy][zz].x=tmp.x;
				pre[xx][yy][zz].y=tmp.y;
				pre[xx][yy][zz].z=tmp.z;
				q.push(next);
				fl[xx][yy][zz]=1;	
			} 
		}
	}
} 
int main()
{
	while(scanf("%d%d%d",&L,&R,&C)&&L)
	{
		fag=0;
		
		memset(fl,0,sizeof(fl)); 
		memset(pre,0,sizeof(pre)); 
		
		getchar();
		char c;
	for(int k=0;k<L;k++)
	{
		for(int i=0;i<R;i++)
		{
			for(int j=0;j<C;j++) 
			{
				cin>>c;
				if(c=='#') mp[k][i][j]=1;
				else mp[k][i][j]=0;
				if(c=='E')
				{
					gl=k;gr=i;gc=j;
				}
				if(c=='S')
				{
					sl=k;sr=i;sc=j;
				}
			}
			getchar();
		}
		getchar();
	}
	bfs();
	int cnt=0;
	int x=sl,y=sr,z=sc,i,j,k;
	
	if(fag)  
	{
		while(x!=gl||y!=gr||z!=gc)
    {
        i = pre[x][y][z].x;
        j = pre[x][y][z].y;
        k = pre[x][y][z].z;
        cnt++;
        x = i;
        y = j;
        z = k;
    }
    printf("Escaped in %d minute(s).\n",cnt);
	}
	else puts("Trapped!");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jack_jxnu/article/details/80980230