BFS廣度優先搜尋(3)--poj2251(zoj1940)(基礎題)

Sly_461發表於2016-09-12
Dungeon Master

                                                 Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu

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!


         這道題題意:一個三維的地牢,給你起點和終點,問從起點到終點至少需要多長時間,每移動一次耗時1minute;如果不能到達終點,輸出“Trapped!“。因為是三維的,所以從 每個點開始搜尋時都會有6個方向,設定6個方向向量,然後每搜到一個點就判斷是不是終點。一個簡單的BFS。 下面是我的AC程式碼:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char map[30][30][30];
int vis[30][30][30];
int L,R,C;
int d[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};   //6個方向
struct node {
	int x,y,z;
	int step;
};
int  Bfs(int x,int y,int z){
	int i,j;
	memset(vis,0,sizeof(vis));
	queue<node>q;
	node s,e;
	s.x=x;
	s.y=y;
	s.z=z;
	s.step=0;
	q.push(s);
	while(!q.empty()){
		s=q.front();
		q.pop();
		if(map[s.x][s.y][s.z]=='E')return s.step;
		for(i=0;i<6;i++){
			int xx=s.x+d[i][0];
			int yy=s.y+d[i][1];
			int zz=s.z+d[i][2];
			if(xx<0||yy<0||zz<0||xx>=L||yy>=R||zz>=C)
				continue;
			if(map[xx][yy][zz]=='#')continue;
			if(vis[xx][yy][zz])continue;
			vis[xx][yy][zz]=1;
			e.x=xx;
			e.y=yy;
			e.z=zz;
			e.step=s.step+1;
			q.push(e);
		}
	}
	return -1;     //不能到達終點返回-1
}
int main()
{
	int i,j,k;
	while(~scanf("%d %d %d",&L,&R,&C)){
		if(!L&&!R&&!C)break;
		for(i=0;i<L;i++){
			for(j=0;j<R;j++){
				scanf("%s",map[i][j]);
			}
		}
		int ans=0;
		for(i=0;i<L;i++){
			for(j=0;j<R;j++){
				for(k=0;k<C;k++){
					if(map[i][j][k]=='S')
					{
						ans=Bfs(i,j,k);
					}
				}
			}
		}
		if(ans==-1)printf("Trapped!\n");
		else printf("Escaped in %d minute(s).\n",ans);
	}
    return 0;
}


相關文章