-
Notifications
You must be signed in to change notification settings - Fork 0
/
astar_algorithm.cpp
98 lines (94 loc) · 2.76 KB
/
astar_algorithm.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "astar_algorithm.h"
#include "mnode.h"
#include <chrono>
#include <functional>
#include <queue>
#include <thread>
#include <utility>
using std::priority_queue;
using std::make_pair;
int AStarAlgorithm::Heuristic(const MNode& nodeA, const MNode& nodeB) const
{
return std::abs(nodeA.mapIndex_X - nodeB.mapIndex_X) + std::abs(nodeA.mapIndex_Y - nodeB.mapIndex_Y);
}
map<int, int> AStarAlgorithm::Resolve()
{
Map& aMap = GetMap();
const MNode& startNode = GetStartNode();
const MNode& endNode = GetEndNode();
map<int, int> solveMap;
map<int, int> lessCostMap;
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size() < 0)
{
stringstream ss;
ss<<"invalid start node:"<<startNode.ToString()<<" or end node:"<<endNode.ToString()<<endl;
cout<<ss.str();
endwin();
return solveMap;
}
priority_queue<MNode, vector<MNode>, std::greater<MNode>> waveQueue;
waveQueue.push(startNode);
lessCostMap.insert({aMap.GetNodeNum(startNode), 0});
while (!waveQueue.empty())
{
const MNode checkNode = waveQueue.top();
waveQueue.pop();
if(checkNode == endNode)
{
break;
}
list<MNode> neighborList = aMap.GetFilterNeighbors(checkNode);
for (MNode nextNode : neighborList)
{
int ckNodeNum = aMap.GetNodeNum(checkNode);
int nextNodeNum = aMap.GetNodeNum(nextNode);
int nextCost = lessCostMap[ckNodeNum] + aMap.GetCost(checkNode, nextNode);
if(lessCostMap.find(nextNodeNum) == lessCostMap.cend() || nextCost < lessCostMap[nextNodeNum])
{
lessCostMap[nextNodeNum] = nextCost;
int heCost = nextCost + Heuristic(nextNode, endNode);
nextNode.CurHeCostSetter(heCost);
if(nextNode != startNode && nextNode != endNode)
{
aMap.Draw(nextNode.heCost, nextNode.mapIndex_Y, nextNode.mapIndex_X);
refresh();
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
waveQueue.push(nextNode);
solveMap.insert(make_pair(nextNodeNum, ckNodeNum));
}
}
}
return solveMap;
}
vector<int> AStarAlgorithm::FindPath(map<int, int>& solveMap)
{
const Map& aMap = GetMap();
const MNode& startNode = GetStartNode();
const MNode& endNode = GetEndNode();
vector<int> pathVec;
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size()<=0)
{
stringstream ss;
ss<<"invalid startNode:"<<startNode.ToString()<<", or endNode:"<<endNode.ToString()<<endl;
cout<<ss.str();
endwin();
return pathVec;
}
int checkNodeNum = aMap.GetNodeNum(endNode);
while (solveMap.find(checkNodeNum) != solveMap.cend())
{
pathVec.push_back(checkNodeNum);
checkNodeNum = solveMap.find(checkNodeNum)->second;
}
//有路径
if(checkNodeNum == aMap.GetNodeNum(startNode))
{
pathVec.push_back(checkNodeNum);
}
else //没有找到
{
pathVec.clear();
}
return pathVec;
}