-
Notifications
You must be signed in to change notification settings - Fork 7
/
AStar.h
83 lines (73 loc) · 1.99 KB
/
AStar.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include "Common.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;
using std::vector;
const int VerticalDist = 10; // 每格到相邻格直线距离
const int ObliqueDist = 14; // 每格到相邻格斜线距离
class AStar
{
public:
struct Node
{
unsigned int X; // 水平坐标
unsigned int Y; // 垂直坐标
unsigned int G, H, F;
bool IsObstacle; // 是否为障碍
bool IsInPath; // 是否是最短路径中的一点
bool IsInClosedList; //
bool IsInOpenedList;
Node *ParentNode;
Node()
{
X = 0;
Y = 0;
G = H = F = 0;
IsObstacle = false;
IsInPath = false;
IsInClosedList = IsInOpenedList = false;
ParentNode = nullptr;
}
void Reset()
{
G = H = F = 0;
IsInPath = false;
IsInClosedList = IsInOpenedList = false;
ParentNode = nullptr;
}
void UpdateF()
{
F = G + H;
}
};
explicit AStar();
virtual ~AStar();
void InitMap(int **map, unsigned int width, unsigned int height);
void FindPath(unsigned int beginX, unsigned int beginY,
unsigned int endX, unsigned int endY);
void PrintPath();
void PrintPathMap();
private:
// 重置地图
void resetMap();
// 该点是否为障碍
bool isObstacle(const Node &node);
// 位置是否存在障碍
bool whetherPosHasObstacle(unsigned int x, unsigned int y);
// 计算h值
int getH(const Node &node);
void updateOpenedNodeList(const Node &exploringNode);
void deleteMap();
private:
// 建立辅助地图
Node **m_map;
unsigned int m_mapWidth;
unsigned int m_mapHeight;
Node *m_beginNode;
Node *m_endNode;
vector<Node *> m_openedNodeList; // 开放节点列表
vector<Node *> m_pathNodeList; // 储存最终路径
};