c++完整实现地图寻路A星算法

A星寻路算法的讲解有很多,这里不再论述,只给出实现程序。

AStar.h

#pragma once

#include <vector>
#include <map>
#include <queue>
// 二维地图A*算法实现

const int OBLIQUE = 14;  // 斜线移动权重为14
const int STEP = 10;
struct Point
{
	Point(int id, int reachable);
	~Point();

	void CalcF() { m_F = m_G + m_H; }
	bool IsReachable()
	{
		if (m_reachable == 0) return true;
		return false;
	}
	bool operator>(const Point* point) // 优先队列小顶堆
	{
		if (m_F > point->m_F)
		{
			return true;
		}
		return false;
	}

	int m_F{ 0 };  // F = G + H
	int m_G{ 0 };  // G 表示从起点 A 移动到网格上指定方格的移动耗费 (可沿斜方向移动)
	int m_H{ 0 };  // H 表示从指定的方格移动到终点 B 的预计耗费 (H 有很多计算方法, 这里我们设定只可以上下左右移动)
	int m_id; // 二维数组转换成一维数组的位置编号 0, 1, 2,....
	int m_reachable;  // 0 可达 1 不可达
	Point* m_parent{nullptr};  // 保存父节点
	int m_close{0};  // 0 未加入closelist 1 加入closelist
	int m_x{0};
	int m_y{0};
};


//--------------
//|0 1 3 4 5
//|6 7 8 9 10
//|11 12 13 14 15
//|....
typedef std::vector<Point*> point_vec_type;
typedef std::map<int, Point*> point_map_type;
typedef std::priority_queue<Point*, point_vec_type> point_queue_type;
class AStarNav
{
public:
	AStarNav();
	~AStarNav();

	bool FindPath(int start_id, int end_id, std::vector<Point*>& find_path);
	bool CanReach(int start_id, int end_id);

	bool LoadMap(const char* path);
	int GetPointId(int i, int j);
private:
	void GetAroundPoints(int point_id, point_vec_type& point_set);
	void GetPos(int& i, int
  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值