POJ2251Dungeon Master

该篇文章描述了一种在给定三维地图上使用广度优先搜索(BFS)算法来帮助角色从起点(S)移动到终点(E),同时计算最短时间逃离迷宫的方法。
摘要由CSDN通过智能技术生成

http://poj.org/problem?id=2251

三维BFS

注意输入的 map[hang][lie][ceng]

for ceng

        for hang

                for lie

#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<stdio.h>
using namespace std;
int stx, sty, stz, enx, eny, enz;
int l, r, c;
char map[100][100][100];
int vis[100][100][100];
int dx[] = { 1,0,-1,0,0,0 }, dy[] = { 0,1,0,-1,0,0 }, dz[] = { 0,0,0,0,1,-1 };
struct Node
{
	int x, y, z, step;
};
queue<Node>q;
void bfs()
{
	while (!q.empty())
	{
		q.pop();
	}
	q.push({ stx,sty,stz,0 });
	vis[stx][sty][stz] = 1;
	while (!q.empty())
	{
		Node p = q.front();
		q.pop();
		if (p.x == enx && p.y == eny && p.z == enz)
		{
			printf("Escaped in %d minute(s).\n", p.step);
			return;
		}
		for (int i = 0; i < 6; i++)
		{
			int nx = p.x + dx[i];
			int ny = p.y + dy[i];
			int nz = p.z + dz[i];
			int ns = p.step + 1;
			if (nx < 0 || ny < 0 || nz < 0 || nx >= r || ny >= c || nz >= l || vis[nx][ny][nz]||map[nx][ny][nz]=='#')continue;
			else
			{
				q.push({ nx,ny,nz,ns });
				vis[nx][ny][nz] = 1;
			}
		}
	}
	cout << "Trapped!" << endl;
}
int main() {
	while (cin >> l >> r >> c)
	{
		memset(vis, 0, sizeof(vis));
		if (l == 0 && r == 0 && c == 0)
			break;
		for (int z = 0; z < l; ++z)//层 
		{
			for (int x = 0; x < r; ++x)//行 
			{
				for (int y = 0; y < c; ++y)//列 
				{
					cin >> map[x][y][z];
					if (map[x][y][z] == 'S')
						stx = x, sty = y, stz = z;
					else if (map[x][y][z] == 'E')
						enx = x, eny = y, enz = z;
				}
			}
		}
		bfs();
	}
}//senak

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值