astar的cpp实现

这是一个关于C++实现A*寻路算法的博客。文章详细介绍了如何使用AStar类来构造路径,并展示了如何在二维网格地图上进行寻路。通过设置障碍物并打印地图,说明了算法在寻找目标路径时的行为。
摘要由CSDN通过智能技术生成

#pragma once
#include
#include
#include
#include
#include
using namespace std;
class AStar
{
public:
struct AStarPathPoint {
AStarPathPoint* father;
int x, y, gval, hval, fval;
AStarPathPoint(pair<int, int> point, pair<int, int> target, AStarPathPoint* parent = NULL)
{
x = point.first;
y = point.second;
father = parent;
if (parent == nullptr) {//没有父节点时
gval = 0;
}
else {
//计算gval\hval\fval
if (abs(father->x - x) + abs(father->y - y) == 1) {
gval = father->gval + 10;
}
else {
gval = father->gval + 15;
}
}
//本例中使用的hval计算方法为对角线计算,本例中的移动方式包含了对角线的移动
hval = InitHavl(point, target);
fval = gval + hval;

	}
	bool operator<(const AStarPathPoint& a) const
	{
		return fval > a.fval; //大顶堆
	}

	inline static int InitHavl(pair<int, int> point, pair<int, int> target) {
		float dx = std::abs(point.first - target.first);
		float dy = std::abs(point.second - target.second);
		//return dx > dy ? (dx - dy) * 10 + dy * 15 : (dy - dx) * 10 + dx * 15;
		return dx * 10 + dy * 10;
	}

};
struct cmp {

	bool  operator ()  (const AStarPathPoint* a, const AStarPathPoint* b) {
		return a->fval > b->fval;
	}
};
AStar(pair<int, int>str, pair<int, int>end);
~AStar();
void HandleNextPoint(AStarPathPoint* cur, vector<vector<int>>&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值