#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>>&