-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbfs_algorithm.cpp
91 lines (87 loc) · 2.43 KB
/
gbfs_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
#include "gbfs_algorithm.h"
#include "mnode.h"
#include <chrono>
#include <functional>
#include <queue>
#include <thread>
using std::priority_queue;
int GBFSAlgorithm::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> GBFSAlgorithm::Resolve()
{
Map& aMap = GetMap();
const MNode& startNode = GetStartNode();
const MNode& endNode = GetEndNode();
map<int, int> solveMap;
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);
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 checkNodeNum = aMap.GetNodeNum(checkNode);
int nextNodeNum = aMap.GetNodeNum(nextNode);
if(solveMap.find(nextNodeNum) == solveMap.cend() && nextNode != startNode)
{
nextNode.CurHeCostSetter(Heuristic(nextNode, endNode));
waveQueue.push(nextNode);
solveMap.emplace(nextNodeNum, checkNodeNum);
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));
}
}
}
return solveMap;
}
vector<int> GBFSAlgorithm::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;
}