POJ2251(Dungeon Master)

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 43228   Accepted: 16346

Description

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水题,但是我TLE了很多次,貌似是不应该使用标记,应该直接改字符串数组中的符号。这样优化到了32ms,一道水题做了2个小时,TLE8次才过,也是醉了~

上代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int l,r,c;
int flag;
int ans;
int ex,ez,ey;
char a[35][35][35];
int dz[6]={0,0,0,0,1,-1};
int dx[6]={0,0,1,-1,0,0};
int dy[6]={1,-1,0,0,0,0};
typedef struct 
{
	int z1;
	int x1;
	int y1;
	int s;
} P;
void bfs(int z,int x,int y)
{
    queue<P> q;
    P p;
    p.z1=z;
    p.x1=x;
    p.y1=y;
    p.s=0;
    q.push(p);
    while(!q.empty())
    {
        P tp=q.front();
        q.pop();
        if(tp.x1==ex&&tp.z1==ez&&tp.y1==ey)
        {
        	flag=1;
        	ans=tp.s;
        	break;
        }
        for(int i=0;i<6;i++)
        {
        	int zz=tp.z1+dz[i];
        	int xx=tp.x1+dx[i];
        	int yy=tp.y1+dy[i];
        	if(a[zz][xx][yy]=='.'||a[zz][xx][yy]=='E')
        	{
        		P pp;
        		pp.z1=zz;
        		pp.x1=xx;
        		pp.y1=yy;
        		pp.s=tp.s+1;
        		a[zz][xx][yy]='*';
        		q.push(pp);
        	}
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&l,&r,&c))
    {
        memset(a,0,sizeof(a));
    	int tx,ty,tz;
    	if(l==0&&r==0&&c==0)
    	break;
        for(int i=1;i<=l;i++)
        {
			for(int j=1;j<=r;j++)
        	{
        		getchar();
				for(int k=1;k<=c;k++)
        		{
				  scanf("%c",&a[i][j][k]);
        		  if(a[i][j][k]=='S')
				   {
				   	   tz=i;
				   	   tx=j;
				   	   ty=k;
				   }
				    if(a[i][j][k]=='E')
				   {
				   	   ez=i;
				   	   ex=j;
				   	   ey=k;
				   }
			    }
			}
            getchar();
        }
		flag=0;
        bfs(tz,tx,ty);
        if(flag==1)
        {
            printf("Escaped in %d minute(s).\n",ans);
        }
        else
        {
        	printf("Trapped!\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/star_moon0309/article/details/79783391