-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
65 lines (54 loc) · 2.21 KB
/
Dijkstra.cpp
File metadata and controls
65 lines (54 loc) · 2.21 KB
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
#include "Dijkstra.hpp"
#include <limits>
struct DijkstraCompare {
bool operator()(const std::shared_ptr<SearchNode>& a,
const std::shared_ptr<SearchNode>& b) {
return a->cost > b->cost; // Минимальная стоимость сначала
}
};
std::vector<Situation> dijkstraSearch(const Situation& start) {
using NodePtr = std::shared_ptr<SearchNode>;
std::priority_queue<NodePtr,
std::vector<NodePtr>,
DijkstraCompare> pq;
std::unordered_map<Situation, int, Situation::Hash> cost_map;
std::unordered_map<Situation, NodePtr, Situation::Hash> parent_map;
// Инициализация
auto startNode = std::make_shared<SearchNode>(start, nullptr, 0);
pq.push(startNode);
cost_map[start] = 0;
while (!pq.empty()) {
auto current = pq.top();
pq.pop();
// Проверка цели
if (current->state.isGoal()) {
std::vector<Situation> path;
auto node = current;
while (node) {
path.push_back(node->state);
node = node->parent;
}
std::reverse(path.begin(), path.end());
return path;
}
// Пропускаем устаревшие узлы
if (current->cost > cost_map[current->state]) continue;
// Генерация следующих состояний
auto nextStates = current->state.generateNextStates();
for (const auto& nextState : nextStates) {
// Расчет новой стоимости
int moveCost = current->state.getMoveCost(nextState);
int newCost = current->cost + moveCost;
// Проверка на улучшение пути
auto it = cost_map.find(nextState);
if (it == cost_map.end() || newCost < it->second) {
cost_map[nextState] = newCost;
auto newNode = std::make_shared<SearchNode>(
nextState, current, newCost);
pq.push(newNode);
parent_map[nextState] = current;
}
}
}
return {}; // Путь не найден
}