国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

算法訓練之BFS POJ 2251 Dungeon Master 三維最短路徑

2019-11-06 06:11:01
字體:
來源:轉載
供稿:網友
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27195 Accepted: 10668

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

Ulm Local 1997

典型的最短路徑問題,將二維的BFS擴展到三維即可。

在此給出AC代碼:

//三維bfs #include<stdio.h>#include<iostream>#include<queue>using namespace std;const int maxn=31;int map[maxn][maxn][maxn];int vis[maxn][maxn][maxn];int sx,sy,sz,ex,ey,ez;int c,a,b;int dirx[6]= {-1,1,0,0,0,0};int diry[6]= {0,0,-1,1,0,0};int dirz[6]= {0,0,0,0,-1,1};struct	node{	int x,y,z,step;};int BFS(){	queue<node>q;	node p1;	p1.x=sx;	p1.y=sy;	p1.z=sz;	vis[sx][sy][sz]=1;	p1.step=0;	q.push(p1);	//printf("%d %d %d/n",p1.x,p1.y,p1.z);	while(!q.empty())	{		node f=q.front(),temp;		q.pop();		if(f.x==ex&&f.y==ey&&f.z==ez)			return f.step;		for(int i=0; i<6; i++)		{			temp.x=f.x+dirx[i];			temp.y=f.y+diry[i];			temp.z=f.z+dirz[i];			temp.step=f.step+1;			if(temp.x<a&&temp.x>=0&&temp.y<b&&temp.y>=0&&temp.z<c&&temp.z>=0&&			        !vis[temp.x][temp.y][temp.z]&&(map[temp.x][temp.y][temp.z]==1))			{				q.push(temp);				//	printf("%d %d %d/n",temp.x,temp.y,temp.z);				vis[temp.x][temp.y][temp.z]=1;			}		}	}	return -1;}int main(){	while(~scanf("%d%d%d",&c,&a,&b),a,b,c)	{		char temp;		for(int i=0; i<c; i++)//平面坐標			for(int j=0; j<a; j++)//j是平面x				for(int k=0; k<b; k++)//k是平面y				{					vis[j][k][i]=0;					cin>>temp;					if(temp=='.')						map[j][k][i]=1;					else if(temp=='#')						map[j][k][i]=0;					else if(temp=='S')					{						sx=j;						sy=k;						sz=i;						map[j][k][i]=1;					}					else if(temp=='E')					{						ex=j;						ey=k;						ez=i;						map[j][k][i]=1;					}				}		int  res=BFS();		if(res==-1)			printf("Trapped!/n");		else			printf("Escaped in %d minute(s)./n",res);	}	return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 印江| 巩留县| 永济市| 东平县| 眉山市| 重庆市| 青田县| 吉水县| 元江| 兴宁市| 兴文县| 高州市| 中阳县| 龙南县| 新郑市| 天峻县| 宁城县| 桐柏县| 巨鹿县| 绥化市| 南投县| 边坝县| 肇东市| 新建县| 东乌| 黄骅市| 周宁县| 历史| 和硕县| 长岭县| 墨竹工卡县| 克东县| 仲巴县| 德清县| 仙游县| 宁乡县| 云阳县| 华阴市| 林口县| 平安县| 临清市|