1. 程式人生 > >POJ2251 Dungeon Master(三維BFS)

POJ2251 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!

好久沒訓練了啊,這樣可不行。。

今天程式設計師過節了呢,可是沒有一點開心的感覺。。

題意:給出a層,每層有b行c列。每次可以上下左右前後6個方向移動一個單位,S為起點,E為終點,問最少需要多少單位時間能到達終點,如果不能則輸出Trapped!

解題思路:雖然比二維bfs多一個方向,但還是基礎的bfs,注意6個方向的編寫就行了。

AC程式碼:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

char mp[35][35][35];
int a,b,c,book[35][35][35];
int nx[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};

struct node
{
	int x,y,z,step;
};

node getnode(int x,int y,int z,int step)
{
	node q;
	q.x=x;
	q.y=y;
	q.z=z;
	q.step=step;
	return q;
}

void bfs(int x,int y,int z,int step)
{
	queue<node> q;
	q.push(getnode(x,y,z,step));
	while(!q.empty())
	{
		for(int i=0;i<6;i++)
		{
			int tx=q.front().x+nx[i][0];
			int ty=q.front().y+nx[i][1];
			int tz=q.front().z+nx[i][2];
			if(tx>=0&&tx<a&&ty>=0&&ty<b&&tz>=0&&tz<c&&mp[tx][ty][tz]!='#'&&book[tx][ty][tz]==0)
			{
				book[tx][ty][tz]=1;
				if(mp[tx][ty][tz]=='E')
				{
					printf("Escaped in %d minute(s).\n",q.front().step+1);
					return;
				}
				q.push(getnode(tx,ty,tz,q.front().step+1));
			}
		}
		q.pop();
	}
	printf("Trapped!\n");
}

int main()
{
	while(~scanf("%d%d%d",&a,&b,&c)&&(a+b+c))
	{
		for(int i=0;i<a;i++)
		{
			for(int j=0;j<b;j++)
			{
				scanf("%s",mp[i][j]);
			}
		}
		memset(book,0,sizeof(book));
		int flag=0;//實驗證明提前跳出迴圈可以節約大量時間
		for(int i=0;i<a;i++)
		{
			for(int j=0;j<b;j++)
			{
				for(int k=0;k<c;k++)
				{
					if(mp[i][j][k]=='S')
					{
						book[i][j][k]=1;
						bfs(i,j,k,0);
						flag=1;
					}
				}
				if(flag) break;
			}
			if(flag) break;
		}
	}
	return 0;
}