-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle4.cpp
79 lines (65 loc) · 1.94 KB
/
oracle4.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
#include <iostream>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <vector>
using namespace std;
struct Edge {
string node;
int weight;
};
vector<string> oracleSearch(const unordered_map<string, vector<Edge>>& graph, const string& start, const string& goal) {
unordered_map<string, bool> visited;
unordered_map<string, string> parent;
unordered_map<string, int> g_values;
queue<string> q;
q.push(start);
visited[start] = true;
g_values[start] = 0;
while (!q.empty()) {
string node = q.front();
q.pop();
if (node == goal) {
vector<string> path;
while (node != start) {
path.push_back(node);
node = parent[node];
}
path.push_back(start);
reverse(path.begin(), path.end());
return path;
}
for (const Edge& edge : graph.at(node)) {
string neighbor = edge.node;
int weight = edge.weight;
if (!visited[neighbor]) {
q.push(neighbor);
visited[neighbor] = true;
parent[neighbor] = node;
g_values[neighbor] = g_values[node] + weight;
}
}
}
return {};
}
int main() {
unordered_map<string, vector<Edge>> graph;
graph["S"] = {{"A", 1}, {"D", 5}};
graph["A"] = {{"B", 3}};
graph["B"] = {{"C", 1}, {"G", 1}};
graph["C"] = {{"G", 1}};
graph["D"] = {{"G", 1}};
string start = "S";
string goal = "G";
vector<string> path = oracleSearch(graph, start, goal);
if (!path.empty()) {
cout << "Path from " << start << " to " << goal << ": ";
for (const string& node : path) {
cout << node << " -> ";
}
cout << goal << endl;
} else {
cout << "No path found." << endl;
}
return 0;
}