-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijstra_algorithm.cpp
103 lines (99 loc) · 2.87 KB
/
dijstra_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
99
100
101
102
103
#include "dijstra_algorithm.h"
#include "mnode.h"
#include <chrono>
#include <cmath>
#include <functional>
#include <ncurses.h>
#include <queue>
#include <thread>
#include <utility>
#include <unordered_map>
using std::priority_queue;
using std::make_pair;
using std::unordered_map;
int DIJAlgorithm::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> DIJAlgorithm::Resolve()
{
Map& aMap = GetMap();
const MNode& startNode = GetStartNode();
const MNode& endNode = GetEndNode();
map<int, int> solveMap;
unordered_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.emplace(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;
nextNode.CurHeCostSetter(nextCost);
if(nextNode!=startNode && nextNode!=endNode)
{
// nextNode.NStateSetter(ENodeState::FINDDING);
// aMap.Draw(nextNode, EDrawType::STATE);
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> DIJAlgorithm::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;
}