1. 程式人生 > >Dungeon Master (廣搜,bfs)

Dungeon Master (廣搜,bfs)

Dungeon Master

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 64   Accepted Submission(s) : 29
Problem 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!

題目翻譯:

有一個三維迷宮,出發點是S,出口是E,第一行三個數字分別代表 高z,行x,列y。‘.’為路,‘#’為牆。每點可以朝三維的6個方向走,不走斜角。

如果走得出來輸出時間(步數),否則。。。

思路:

廣搜的模板題,將二維迷宮直接擴充套件為三維座標即可:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
#define INF 0xfffffff//先設為個很大的數
int x,y,z,ex,ey,ez,ans,vis[35][35][35],L,R,C,sign;
int dx[6]={0,1,-1,0,0,0};//六個方向
int dy[6]={1,0,0,-1,0,0};
int dz[6]={0,0,0,0,1,-1};
int map[30][30][30];
struct node
{
	int x,y,z,step;//座標,已走步數
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;
	}
}a,temp;//a為當前,temp為下一步
int jud(struct node a)//判斷是否可以走
{
	if(a.x<0||a.x>C)
		return 0;
	if(a.y<0||a.y>R)
		return 0;
	if(a.z<0||a.z>L)
		return 0;
	if(vis[a.x][a.y][a.z]||map[a.x][a.y][a.z])
		return 0;
	if(temp.step>=ans)
		return 0;
	return 1;
}
void bfs()
{
	a.x=x;
	a.y=y;
	a.z=z;
	a.step=0;
	memset(vis,0,sizeof(vis));
	priority_queue<node>q;//優先佇列
	vis[x][y][z]=1;
	q.push(a);
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		for(int i=0;i<6;i++)
		{
			temp.x=a.x+dx[i];//6個方向試探
			temp.y=a.y+dy[i];
			temp.z=a.z+dz[i];
			temp.step=a.step+1;
			if(jud(temp))
			{
				if(temp.x==ex&&temp.y==ey&&temp.z==ez)
				{
				//	if(ans>temp.step)
						sign=1;//到達E點標記為1
						ans=temp.step;
						return;
				//	continue;
				}
				vis[temp.x][temp.y][temp.z]=1;//記錄此點已被訪問
				q.push(temp);
			}
		}
	}
}
int main()
{
	char u;
	while(scanf("%d%d%d",&L,&R,&C),L|R|C)
	{
		for(int l=0;l<L;l++)
		{
			for(int r=0;r<R;r++)
			{
				getchar();//吸收換行符
				for(int c=0;c<C;c++)
					{
						scanf("%c",&u);//講字元陣轉換為0-1矩陣
						if(u=='.')
						map[r][c][l]=0;
						else if(u=='#')
						map[r][c][l]=1;
						else if(u=='S')
						{
							x=r,y=c,z=l;//起點座標記錄
							map[r][c][l]=0;
						}
						else if(u=='E')
						{
							ex=r,ey=c,ez=l;//終點座標記錄
							map[r][c][l]=0;
						}
						
					}
			}
			getchar();//吸收空行
		}
//		for(int l=0;l<L;l++)//檢視下轉換的矩陣寫對沒
//		{
//			for(int r=0;r<R;r++)
//			{
//				for(int c=0;c<C;c++)
//					printf("%d",map[r][c][l]);
//				putchar('\n');
//			}
//			putchar('\n');
//		}

		if(x==ex&&y==ey&&z==ez)
		{
			printf("0\n");
			continue;
		}
		ans=INF;
		sign=0;
		bfs();
		if(sign)
		printf("Escaped in %d minute(s).\n",ans);
		else
		printf("Trapped!\n");
	}
}