-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStar.cpp
More file actions
41 lines (35 loc) · 1.4 KB
/
AStar.cpp
File metadata and controls
41 lines (35 loc) · 1.4 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
#include "AStar.hpp"
std::vector<Situation> aStarSearch(const Situation& start) {
auto cmp = [](const std::shared_ptr<SearchNode>& a, const std::shared_ptr<SearchNode>& b) {
return a->totalCost() > b->totalCost();
};
std::priority_queue<std::shared_ptr<SearchNode>, std::vector<std::shared_ptr<SearchNode>>, decltype(cmp)> pq(cmp);
std::unordered_set<Situation, Situation::Hash> visited;
std::vector<Situation> path;
auto startNode = std::make_shared<SearchNode>(start, nullptr, 0, start.manhattanDistance());
pq.push(startNode);
visited.insert(start);
while (!pq.empty()) {
auto current = pq.top();
pq.pop();
if (current->state.isGoal()) {
auto node = current;
while (node) {
path.push_back(node->state);
node = node->parent;
}
std::reverse(path.begin(), path.end());
return path;
}
auto nextStates = current->state.generateNextStates();
for (const auto& nextState : nextStates) {
if (visited.find(nextState) == visited.end()) {
visited.insert(nextState);
int heuristic = nextState.manhattanDistance();
auto newNode = std::make_shared<SearchNode>(nextState, current, current->cost + 1, heuristic);
pq.push(newNode);
}
}
}
return {};
}