POJ 2251 Dungeon Master(bfs)

先介绍队列的定义   queue<类型名>变量名,如

queue<int>q,queue或queue<node>que;(struct node(结构体类型));

1、 q.front()      取出队首元素

2、   q.empty()    判断是否为空,时空返回1

3、   q.pop()       删除队首元素

4、   q.push(变量)加入变量

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
bool visit[35][35][35];
char map[35][35][35];
int dir[6][3]={1 , 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};
int l, m, n;
struct node{
	int x, y, z;
	int step;
};
int bfs(int x, int y, int z){
	memset(visit, false, sizeof(visit));
	node u;
	u.x = x, u.y = y, u.z = z, u.step = 0;
	queue<node> q;
	q.push(u);
	while(!q.empty()){
		u = q.front();
		if(map[u.x][u.y][u.z] == 'E')	return u.step;
		q.pop();
		for(int i = 0; i < 6; ++i){
			node v;
			v.x = u.x + dir[i][0];
			v.y = u.y + dir[i][1];
			v.z = u.z + dir[i][2];
			if(!visit[v.x][v.y][v.z] && map[v.x][v.y][v.z] != '#' && v.x >= 0 && v.x < l && v.y >= 0 && v.y < m && v.z >= 0 && v.z < n){
				visit[v.x][v.y][v.z] = true;
				v.step = u.step + 1;
				q.push(v);
			}
		}
	}
	return 0;
}
int main(){
	int sx, sy, sz;
	while(~scanf("%d%d%d%*c", &l, &m, &n) && n && l && m){
		memset(map, 0, sizeof(map));
		for(int i = 0; i < l; ++i)
			for(int j = 0; j < m; ++j){
				scanf("%s", map[i][j]);
				for(int k = 0; k < n; ++k)
					if(map[i][j][k] == 'S'){
						sx = i, sy = j, sz = k;
					}
			}
		int step = bfs(sx, sy, sz);
		if(step)printf("Escaped in %d minute(s).\n", step);
        else printf("Trapped!\n");
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值