-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathFinder.h
52 lines (40 loc) · 1.48 KB
/
PathFinder.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
#pragma once
#include <queue>
struct Node
{
Node(int _index, int _x, int _y, int _parentIndex, int _distanceFromStart, int _weight)
: index(_index)
, x(_x)
, y(_y)
, parentIndex(_parentIndex)
, distanceFromStart(_distanceFromStart)
, weight(_weight)
{}
int index = 0;
int x = 0;
int y = 0;
int parentIndex = 0;
int distanceFromStart = 0;
int weight = 0; // Sum of the distance from start node to current node and distance from target node to curren node
};
bool operator>(const Node& left, const Node& right);
class PathFinder
{
public:
PathFinder(std::pair<int, int> start, std::pair<int, int> target, const std::vector<int>& map, std::pair<int, int> mapDimensions);
~PathFinder() = default;
bool FindPath(std::vector<int>& outPath);
private:
void CalculateData(const Node& node);
void AddNodeToQueue(const Node& parentNode, const int x, const int y);
int FindIndex(const int x, const int y);
int CalculateDistance(const int x, const int y);
private:
std::pair<int, int> _start;
std::pair<int, int> _target;
std::vector<int> _map;
std::pair<int, int> _mapDimensions;
std::priority_queue<Node, std::vector<Node>, std::greater<Node>> _nodesQueue;
std::vector<int> _distances; // For storing distance from the start node
std::vector<int> _parentIndexes; // For helping traverse back from target node
};