码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2251 BFS

时间:2015-08-26 12:05:51      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:bfspan idtransmarksp   pspan idtransmarkspa   

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21312   Accepted: 8283

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!

Source

Ulm Local 1997

给出一三维空间的地牢,要求求出由字符‘S‘到字符‘E‘的最短路径

移动方向可以是上,下,左,右,前,后,六个方向

每移动一次就耗费一分钟,要求输出最快的走出时间。

用BFS求最短路 比DFS好;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>

using namespace std;

struct node
{
	int x,y,z;
}p,s,e,q;
queue<node>Q;
int n,m,h;
char a[100][100][100];  //三维
int vis[100][100][100];
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,-1,1};
int step[100][100][100];
int Judge(node q)
{
	if(q.x>=0&&q.y>=0&&q.z>=0&&q.x<n&&q.y<m&&q.z<h&&a[q.x][q.y][q.z]!='#') //存在E
		return 1;
	return 0;
}
int BFS()
{
	while(!Q.empty())   //  <span id="transmark"></span>队列必须清空
		Q.pop();
	
	Q.push(s);
	vis[s.x][s.y][s.z]=1;
	step[s.x][s.y][s.z]=0;
	while(!Q.empty())
	{
		p=Q.front();
		Q.pop();
		for(int i=0;i<6;i++)
		{
			q.x=dx[i]+p.x;
			q.y=dy[i]+p.y;
			q.z=dz[i]+p.z;
			if(!Judge(q))
				continue;
			if(vis[q.x][q.y][q.z]==0)
			{
				Q.push(q);
				step[q.x][q.y][q.z]=step[p.x][p.y][p.z]+1;
				vis[q.x][q.y][q.z]=1;
			}
			if(a[q.x][q.y][q.z]=='E')
				return step[q.x][q.y][q.z];
		}
	}
	return -1;
}
int main()
{
	while(~scanf("%d%d%d",&n,&m,&h))
	{
		int flag=1;
		if(!n&&!m&&!h)
		break;
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				scanf("%s",a[i][j]);
				if(flag)
				for(int k=0;k<h;k++)
				{
					if(a[i][j][k]=='S')
					{
						s.x=i;s.y=j;s.z=k;
						flag=0;
						break;
					}
				}
			}
		}
		int x=BFS();
		if(x==-1)
		{
			printf("Trapped!\n");
	        continue;
		}
		printf("Escaped in %d minute(s).\n",x);
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 2251 BFS

标签:bfspan idtransmarksp   pspan idtransmarkspa   

原文地址:http://blog.csdn.net/became_a_wolf/article/details/48000499

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!