代码改变世界

poj 2251 Dungeon Master ----BFS (三维六个方向)

2012-03-08 19:44  java环境变量  阅读(289)  评论(0编辑  收藏  举报
                                       Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10701   Accepted: 4129

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!

 

 

/*
     题目大意: 一个的地牢,有多层。'#'表示墙壁 , '.'表示通道 。相邻两层的'.'之间可以通过。
	 求从S代表的起点到E代表的重点 所需的最短时间。每移动一步用时1分钟。
	 如果逃不出 输出 Trapped! 
	 为了方便  将三维坐标用一维存储。
*/

//Memory: 588 KB   Time: 16 MS  
//Language: C++   Result: Accepted  

#include<stdio.h>
#include<string.h>
void bfs(int m);
int vir[35*35*35],fa[35*35*35];   //数组一定要记得稍微开大一点
char maze[35][35][35];            //刚刚就是最大值都定为30 所以WA了几次。
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int q[35*35*35];
int l,r,c;
int main()
{
	//freopen("1.txt","r",stdin);
	int i,j,k,start;
	while(scanf("%d%d%d",&l,&r,&c)!=EOF&&(l||r||c))
	{
		int t=1;
		memset(vir,0,sizeof(vir));
		for(i=0;i<l;i++)
			for(j=0;j<r;j++)
			{
				scanf("%s",maze[i][j]);
				if(t)
				{
					for(k=0;k<c;k++)
						if(maze[i][j][k]=='S') {  //几下起点。
						t=0;
						start=i*r*c+j*c+k;
					}
				}
			}
		bfs(start);
	}
	return 0;
}
//BFS函数
void bfs(int m)
{
	int front=0,rear=0,x,y,z,nx,ny,nz,nm,i,count=0;
	fa[m]=m;
	vir[m]=1;
	q[rear++]=m;  //第一个元素入队
	while(front<rear)
	{
		m=q[front++];  //首元素出队
		z=m/(r*c);       //将m表示的三维坐标求出
		x=(m%(r*c))/c;
		y=m%c;
		for(i=0;i<6;i++)  //六个方向扩展
		{
			nx=x+dir[i][0];
			ny=y+dir[i][1];
			nz=z+dir[i][2];
			nm=nx*c+ny+nz*r*c;
			//如果可行 ,入队,标记 ,记下父元素
			if(nx>=0&&nx<r&&ny>=0&&ny<c&&nz>=0&&nz<l&&!vir[nm]&&maze[nz][nx][ny]=='.')
			{
				vir[nm]=1;
				q[rear++]=nm;
				fa[nm]=m;
			}
			if(maze[nz][nx][ny]=='E')  //到了终点
			{
				fa[nm]=m;
				for(;fa[nm]!=nm;nm=fa[nm])  //统计用时
					count++;
				printf("Escaped in %d minute(s).\n",count);
				return ;
			}
		}
	}
	printf("Trapped!\n");   //如果可行路径遍历完 都没有找到终点  即无法逃出
}