POJ2251 - Dungeon Master - 广度优先搜索

#include<stdio.h>
#include<string.h>
char map[35][35][35];
int mark[35][35][35];
int to[7][4]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
int a,b,c;
struct
{
	int x,y,z,id;
}queue[27005],st,ed,now,next;
int bfs()
{
	memset(mark,0,sizeof(mark));
	int fro,end;
	fro=end=0;
	st.id=0;
	queue[end++]=st;
	mark[st.x][st.y][st.z]=1;
	while(fro<end)
	{
		now=queue[fro++];
		if(now.x==ed.x&&now.y==ed.y&&now.z==ed.z)
		{
			return now.id;
		}
		for(int i=0;i<6;i++)
		{
			next.x=now.x+to[i][0];
			next.y=now.y+to[i][1];
			next.z=now.z+to[i][2];
			if(next.x<0||next.y<0||next.z<0||next.x>=a||next.y>=b||next.z>=c||mark[next.x][next.y][next.z]==1||map[next.x][next.y][next.z]=='#')
			{
				continue;
			}
			next.id=now.id+1;
			mark[next.x][next.y][next.z]=1;
			queue[end++]=next;
		}
	}
	return -1;
}
int main()
{
	while(scanf("%d%d%d",&a,&b,&c)!=EOF)
	{
		if(a==0&&b==0&&c==0)
		{
			break;
		}
		for(int i=0;i<a;i++)
		{
			if(i!=0)
			{
				getchar();
			}
			for(int j=0;j<b;j++)
			{
				getchar();
				for(int k=0;k<c;k++)
				{
					scanf("%c",&map[i][j][k]);
					if(map[i][j][k]=='S')
					{
						st.x=i;
						st.y=j;
						st.z=k;
					}
					else if(map[i][j][k]=='E')
					{
						ed.x=i;
						ed.y=j;
						ed.z=k;
					}
				}
			}
		}
		int t=bfs();
		if(t!=-1)
		{
			printf("Escaped in %d minute(s).\n",t);
		}
		else
		{
			puts("Trapped!");
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值