POJ2251-Dungeon Master

全解题报告索引目录 -> 【北大ACM – POJ试题分类

转载请注明出处:http://exp-blog.com

-------------------------------------------------------------------------

 

题目大意:
 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径

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

每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的

 

解题思路:

我越看这题就越觉得是  XX地下城 = =

水题一道,求最短路问题,直接BFS得了

开三维数组,每次搜索方向由二维的4个方向增加到6个,但是方法还是那个方法

没难度

 

注意若果三维数组恰好开到极限的30*30*30是会RE的,别替人家电脑省空间,想AC就开大点。

 

值得一提的是。。。这题竟然被那篇经典的  POJ分类  文章归纳到DFS。。。网上发现几个同学还在郁闷地DFS。。。。

这里就提示一下大家,凡是看到求最短路,用DFS一定很难做出来,一定要BFS

 

//Memory Time 
// 784K  32MS 

#include<iostream>
using namespace std;

typedef class
{
	public:
		int l,r,c;
		int depth;  //树深(分钟)
}SE;

SE s,e;
bool maze[40][40][40];
int shortminute;

bool BFS(int k,int i,int j)
{
	bool vist[40][40][40]={false};

	SE queue[30000];
	int head,tail;
	queue[head=0].l=k;
	queue[tail=0].r=i;
	queue[0].c=j;
	queue[tail++].depth=1;

	vist[k][i][j]=true;

	while(head<tail)
	{
		SE x=queue[head++];

		if(x.l==e.l && x.r==e.r && x.c==e.c)
		{
			shortminute=x.depth;
			return true;
		}

		if(maze[x.l][x.r][x.c-1] && !vist[x.l][x.r][x.c-1])  //West
		{
			vist[x.l][x.r][x.c-1]=true;
			queue[tail].l=x.l;
			queue[tail].r=x.r;
			queue[tail].c=x.c-1;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l][x.r-1][x.c] && !vist[x.l][x.r-1][x.c])  //North
		{
			vist[x.l][x.r-1][x.c]=true;
			queue[tail].l=x.l;
			queue[tail].r=x.r-1;
			queue[tail].c=x.c;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l][x.r][x.c+1] && !vist[x.l][x.r][x.c+1])  //East
		{
			vist[x.l][x.r][x.c+1]=true;
			queue[tail].l=x.l;
			queue[tail].r=x.r;
			queue[tail].c=x.c+1;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l][x.r+1][x.c] && !vist[x.l][x.r+1][x.c])  //South
		{
			vist[x.l][x.r+1][x.c]=true;
			queue[tail].l=x.l;
			queue[tail].r=x.r+1;
			queue[tail].c=x.c;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l-1][x.r][x.c] && !vist[x.l-1][x.r][x.c])  //Up
		{
			vist[x.l-1][x.r][x.c]=true;
			queue[tail].l=x.l-1;
			queue[tail].r=x.r;
			queue[tail].c=x.c;
			queue[tail++].depth=x.depth+1;
		}
		if(maze[x.l+1][x.r][x.c] && !vist[x.l+1][x.r][x.c])  //Down
		{
			vist[x.l+1][x.r][x.c]=true;
			queue[tail].l=x.l+1;
			queue[tail].r=x.r;
			queue[tail].c=x.c;
			queue[tail++].depth=x.depth+1;
		}
	}
	return false;
}

int main(int i,int j,int k)
{
	int L,R,C;
	while(cin>>L>>R>>C)
	{
		if(!L && !R && !C)
			break;

		/*Initial*/

		memset(maze,false,sizeof(maze));
		
		/*Structure the Maze*/

		for(k=1;k<=L;k++)
			for(i=1;i<=R;i++)
				for(j=1;j<=C;j++)
				{
					char temp;
					cin>>temp;
					if(temp=='.')
						maze[k][i][j]=true;
					if(temp=='S')
					{
						maze[k][i][j]=true;
						s.l=k;
						s.r=i;
						s.c=j;
					}
					if(temp=='E')
					{
						maze[k][i][j]=true;
						e.l=k;
						e.r=i;
						e.c=j;
					}
				}

		/*Search the min Minute*/

		if(BFS(s.l,s.r,s.c))
			cout<<"Escaped in "<<shortminute-1<<" minute(s)."<<endl;
		else
			cout<<"Trapped!"<<endl;

	}
	return 0;
}


 

 
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值