-
Notifications
You must be signed in to change notification settings - Fork 0
/
HC2.cpp
105 lines (85 loc) · 2.94 KB
/
HC2.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
104
105
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <functional>
using namespace std;
class Graph {
public:
Graph(int numVertices) : numVertices(numVertices) {
adjList.resize(numVertices);
}
void addEdge(char src, char dest, int cost) {
int srcIndex = src - 'A';
int destIndex = dest - 'A';
adjList[srcIndex].push_back(make_pair(dest, cost));
adjList[destIndex].push_back(make_pair(src, cost)); // For an undirected graph
}
bool hillClimbing(char start, char goal, unordered_map<char, char>& parent) {
int startIndex = start - 'A';
int goalIndex = goal - 'A';
priority_queue<pair<int, char>, vector<pair<int, char>>, greater<pair<int, char>>> pq; // Min-heap
unordered_map<char, int> heuristic; // Heuristic values
for (int i = 0; i < numVertices; i++) {
heuristic[char('A' + i)] = estimateHeuristic(char('A' + i), goal);
}
vector<bool> visited(numVertices, false);
visited[startIndex] = true;
parent[start] = '\0';
pq.push(make_pair(heuristic[start], start));
while (!pq.empty()) {
char current = pq.top().second;
pq.pop();
if (current == goal) {
return true; // Found a path to the goal
}
for (pair<char, int> neighbor : adjList[current - 'A']) {
char next = neighbor.first;
int cost = neighbor.second;
if (!visited[next - 'A']) {
visited[next - 'A'] = true;
parent[next] = current;
pq.push(make_pair(heuristic[next] + cost, next));
}
}
}
return false; // No path to the goal
}
int estimateHeuristic(char current, char goal) {
// You can implement your heuristic function here.
// This example uses a simple Manhattan distance heuristic.
return abs(current - goal);
}
private:
int numVertices;
vector<vector<pair<char, int>>> adjList;
};
int main() {
Graph graph(26);
graph.addEdge('S', 'A', 1);
graph.addEdge('S', 'B', 3);
graph.addEdge('A', 'F', 5);
graph.addEdge('B', 'C', 2);
graph.addEdge('B', 'D', 4);
graph.addEdge('C', 'E', 1);
graph.addEdge('D', 'F', 3);
graph.addEdge('E', 'F', 2);
char start = 'S';
char goal = 'F';
unordered_map<char, char> parent;
for (char c = 'A'; c <= 'Z'; c++) {
parent[c] = '\0';
}
if (graph.hillClimbing(start, goal, parent)) {
cout << "Path found!" << endl;
cout << "Path: ";
while (goal != start) {
cout << goal << " <- ";
goal = parent[goal];
}
cout << start << endl;
} else {
cout << "No path found." << endl;
}
return 0;
}