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");
	}
}        


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值