POJ 2551 Dungeon Master

以后还是用容器写吧,手撸队列还是太麻烦了;

一直找不到错在哪里了,

后来才发现眼花啦,三个for循环把输出仍在里面了;

就和线段树当时把预处理仍在里面一样,以后要仔细 仔细 仔细!


大致题意:有一个3D的地牢,让你从S点到E点的最短时间,如果无法到达,就输出不可能;

                  广搜特性,搜出来的就是最短时间;顺带一提是六方向搜索,所以这里的写法是六方向的搜索写法;


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!


AC代码:

(一)手写队列

<strong>#include <iostream>
#include <string.h>
#include <cstdio>
#include <algorithm>
using namespace std ;
int length[30000];
int xx[]={1,-1,0,0,0,0};
int zz[]={0,0,0,0,-1,1};
int yy[]={0,0,-1,1,0,0};
bool dis[40][40][40];
struct q{
	int x ;
	int y ;
	int z ;
}q[30000];
char map[100][100][100];
int l , r , c ,sx,sy,sz;

int bfs()
{
	int rear, front,dx,dy,dz;
	memset(dis,0,sizeof(dis));
	memset(length,0,sizeof(length));
	q[0].x=sx,q[0].y=sy,q[0].z=sz;
	front=rear=0;
	while(front<=rear)
	{
		for(int i=0;i<6;i++)
		{
			dx=q[front].x+xx[i];
			dy=q[front].y+yy[i];
			dz=q[front].z+zz[i];
			if(!dis[dx][dy][dz]&& (map[dx][dy][dz]=='.' || map[dx][dy][dz]=='E') && dx>=0 && dx<l && dy>=0 && dy<r && dz>=0 && dz<c)

			{
				dis[dx][dy][dz]=1;
				rear++; 
				q[rear].x=dx;
				q[rear].y=dy;
				q[rear].z=dz;
				length[rear]=length[front]+1;
				if(map[dx][dy][dz]=='E')
				return length[rear];
			}
		}
		front++;
	}
	return 0 ;
}

int main()
{
	int i,j,k,key;
	while(scanf("%d%d%d",&l,&r,&c))
	{
		getchar();
		if(l==0 && r==0 && c==0) break;

		for(i = 0 ; i < l ; i++,getchar())
		{
			for(j=0;j<r;j++,getchar())
			{
				for(k = 0 ; k < c ;k++ )
				{
					scanf("%c",&map[i][j][k]);
					if(map[i][j][k]=='S')
					{
						sx=i;
						sy=j;
						sz=k;
					}
				}
			}
		}
		key=bfs();
		if(key)	printf("Escaped in %d minute(s).\n",key);
		else printf("Trapped!\n");
	}
}


(二)队列写法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std ;
int xx[]={-1,1,0,0,0,0};
int yy[]={0,0,-1,1,0,0};
int zz[]={0,0,0,0,-1,1};
int l , r , c , sx,sy ,sz;
int map[40][40][40];
struct node {
	int x ;
	int y;
	int z;
	int temp;
};
bool check(int x ,int y ,int z)
{
	if(x<0||y<0||z<0||x>=l||y>=r||z>=c)
	return 0;
	if(map[x][y][z]=='#')
	return 0 ;
	return 1;
}
int bfs()
{
	node p , q ;
	queue<node>Q;
	q.x=sx;q.y=sy;q.z=sz;q.temp=0;
	map[q.x][q.y][q.z]='#';
	Q.push(q);
	while(!Q.empty())
	{
		q=Q.front();
		for(int i = 0 ; i<6;i++)
		{
			p.x=q.x+xx[i];
			p.y=q.y+yy[i];
			p.z=q.z+zz[i];
			p.temp=q.temp+1;
			if(check(p.x,p.y,p.z))
			{
				if(map[p.x][p.y][p.z]=='E')
				{
					return p.temp;
				}
				map[p.x][p.y][p.z]='#';
				Q.push(p);
			}
		}
		Q.pop();
	}
	return -1;
}
int main()  
{  
    int i,j,k,key;  
    while(scanf("%d%d%d",&l,&r,&c))  
   {  
       getchar();  
       if(l==0 && r==0 && c==0) break;  
  
       for(i = 0 ; i < l ; i++,getchar())  
       {  
            for(j=0;j<r;j++,getchar())  
           {  
               for(k = 0 ; k < c ;k++ )  
                {  
                   scanf("%c",&map[i][j][k]);  
                   if(map[i][j][k]=='S')  
                   {  
                       sx=i;  
                       sy=j;  
                       sz=k;  
                   }  
               }  
            }  
        }  
       key=bfs();  
       if(key!=-1) printf("Escaped in %d minute(s).\n",key);  
        else printf("Trapped!\n");  
   }  
}





你可能感兴趣的:(POJ 2551 Dungeon Master)