1. 程式人生 > >poj 2251 Dungeon Master (bfs 模板題 / 三維bfs)

poj 2251 Dungeon Master (bfs 模板題 / 三維bfs)

Dungeon Master

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 51038   Accepted: 19132

題目連結

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啦  直接套就好了  注意這裡是三維。程式碼其實還有個地方的處理我也不是很弄得拎清...就會為啥在'E'的時候不用判是不是訪問過...反正我加了答案就是錯的了..

【程式碼】

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
using namespace std;

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

const int maxn=35;
char mp[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};

int l,r,c;
int x0,y0,z0;

void bfs()
{
	queue<node>q;
	node n1;
	n1.x=x0;n1.y=y0;n1.z=z0;n1.t=0;//初始耗時為0
	q.push(n1);
	while(!q.empty())
	{
		node n2=q.front();
		q.pop();
		int x2=n2.x;
		int y2=n2.y;
		int z2=n2.z;
		int tt=n2.t;
		if(mp[x2][y2][z2]=='E')//這裡不用判斷vis[x2][y2][z2]是否訪問過;不知道為啥 
		{
			printf("Escaped in %d minute(s).\n",tt);
			return;
		}
		for(int i=0;i<6;i++)
		{
			int x3=x2+dir[i][0];
			int y3=y2+dir[i][1];
			int z3=z2+dir[i][2];
			node tmp;
			tmp.x=x3;tmp.y=y3;tmp.z=z3;tmp.t=tt+1;
			if(x3<0 || x3>=l || y3<0 || y3>=r || z3<0 || z3>=c)continue;
			if(mp[x3][y3][z3]!='#' && !vis[x3][y3][z3])
			{
				vis[x3][y3][z3]=1;
				q.push(tmp);
			}
		}
	} 
	puts("Trapped!");
}

int main()
{
	while(~scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
	{
		getchar();
		for(int i=0;i<l;i++)
			for(int j=0;j<r;j++)
				for(int k=0;k<c;k++)
				{
					cin>>mp[i][j][k];//這裡用scanf的話會出問題
					if(mp[i][j][k]=='S')
						x0=i,y0=j,z0=k; 
				}
		memset(vis,0,sizeof(vis));
		bfs();
	}
	return 0;
}