poj 2251 Dungeon Master

本文介绍了 POJ 2251 DungeonMaster 的解题思路,采用三维BFS算法实现迷宫寻路。通过破坏原地图的方式避免使用额外的visit数组,并分享了代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

poj   2251   Dungeon Master                  题目链接:http://poj.org/problem?id=2251

                                                          

三维BFS水

题目大意:这是一个三维地牢,你怎么出去呢?

题目分析:毫无tricker,一遍水过,不必加visit数组,直接破坏原地图就可以,输入要注意吃掉空行。

code:

#include<cstdio>
#include<queue>
using namespace std;
char map[40][40][40];
int m,n,p,sx,sy,sz,ex,ey,ez;
int dir[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
struct node
{
	int x,y,z,step;
};
bool judge(int x,int y,int z)
{
	if(x>=0&&x<m&&y>=0&&y<n&&z>=0&&z<p&&map[x][y][z]!='#')return true;
	else return false;
}
int bfs(int x,int y,int z)
{
	queue<node>que;
	node first,next;
	first.x=x,first.y=y,first.z=z,first.step=0;
	que.push(first);
	while(!que.empty())
	{
		first=que.front();
		que.pop();
		for(int i=0;i<6;i++)
		{
			next.x=first.x+dir[i][0];
			next.y=first.y+dir[i][1];
			next.z=first.z+dir[i][2];
			next.step=first.step+1;
			if(judge(next.x,next.y,next.z))
			{
				que.push(next);
				map[next.x][next.y][next.z]='#';
				//printf("现在在:map[%d][%d][%d],step是%d,ex==%d,ey==%d,ez==%d\n",next.x,next.y,next.z,next.step,ex,ey,ez);
				if(next.x==ex&&next.y==ey&&next.z==ez)return next.step;
			}
		}
	}
	return 0;
}
int main()
{
	int i,j,k;
	while(scanf("%d%d%d",&m,&n,&p)!=EOF&&(m||n||p))
	{
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				getchar();
				scanf("%s",map[i][j]);
				for(k=0;k<p;k++)
				{
					if(map[i][j][k]=='S')sx=i,sy=j,sz=k;
					else if(map[i][j][k]=='E')ex=i,ey=j,ez=k;
				}
			}
		}
		int flag=bfs(sx,sy,sz);
		if(flag)printf("Escaped in %d minute(s).\n",flag);
		else printf("Trapped!\n");
	}
	return 0;
} 


PS:好久没这么顺了,搜索大概可以告一段落了,回归DP的干活!!







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值