-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriceMatch.cpp
More file actions
47 lines (40 loc) · 1.4 KB
/
PriceMatch.cpp
File metadata and controls
47 lines (40 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
42
43
44
45
46
47
#include "PriceMatch.hpp"
struct UniformCompare {
bool operator()(const std::shared_ptr<SearchNode>& a,
const std::shared_ptr<SearchNode>& b) {
return a->cost > b->cost;
}
};
std::vector<Situation> priceMatchSearch(const Situation& start) {
std::priority_queue<std::shared_ptr<SearchNode>,
std::vector<std::shared_ptr<SearchNode>>,
UniformCompare> pq;
std::unordered_set<Situation, Situation::Hash> visited;
std::vector<Situation> path;
auto startNode = std::make_shared<SearchNode>(start, nullptr, 0);
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);
auto newNode = std::make_shared<SearchNode>(
nextState, current, current->cost + 1);
pq.push(newNode);
}
}
}
return {};
}