POJ 2251 Dungeon Master (BFS例题)

用STL的queue实现BFS,最基础的题目了,不多说上代码

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>

#define MAXN 35

using namespace std;

int lv,n,m,sta_x,sta_y,sta_z;
char maze[MAXN][MAXN][MAXN];
int op[7][3] = {{0,0,0},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool mark[MAXN][MAXN][MAXN];

struct node
{
	int z;
	int x;
	int y;
	int step;
};

void find_point()
{
	for(int i = 0;i < lv;i ++)
	{
		for(int j = 0;j < n;j ++)
		{
			for(int k = 0;k < m;k ++)
			{
				if(maze[i][j][k] == 'S')
				{
					sta_x = j; 
					sta_y = k;
					sta_z = i;
				}
			}
		}
	}
}

void bfs()
{
	bool flag = false;
	memset(mark , false , sizeof(mark));
	mark[sta_z][sta_x][sta_y] = true;
	queue<node>myQueue;
	node st;
	st.z = sta_z; st.x = sta_x; st.y = sta_y; st.step = 0;
	myQueue.push(st);
	while(!myQueue.empty())
	{
		node tmp = myQueue.front();
		myQueue.pop();
		for(int i = 1;i <= 6;i ++)
		{
			int tz = tmp.z + op[i][0];
			int tx = tmp.x + op[i][1];
			int ty = tmp.y + op[i][2];
			if(tz >=0 && tz < lv && tx >=0 && tx < n && ty >=0 && ty < m && !mark[tz][tx][ty])
			{
				if(maze[tz][tx][ty] != '#')
				{
					if(maze[tz][tx][ty] == 'E')
					{
						printf("Escaped in %d minute(s).\n",tmp.step + 1);
						return ;
					}
					mark[tz][tx][ty] = true; node now;
					now.z = tz; now.x = tx; now.y = ty;
					now.step = tmp.step + 1;
					myQueue.push(now);
				}
			}
		}
	}
	printf("Trapped!\n");
}

int main()
{
	while(cin>>lv>>n>>m && (lv || n || m))
	{
		for(int i = 0;i < lv;i ++)
			for(int j = 0;j < n;j ++)
				scanf("%s",maze[i][j]);
		find_point();
		bfs();
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值