-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
119 lines (100 loc) · 3.2 KB
/
Copy pathcode.cpp
File metadata and controls
119 lines (100 loc) · 3.2 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <bits/stdc++.h>
using namespace std;
int N = 3; // Size of the puzzle
// Manhattan Distance Heuristic function
int manhattanDistance(vector<vector<int>>& state, vector<vector<int>>& goal) {
int distance = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (state[i][j] != 0) {
for (int x = 0; x < N; ++x) {
for (int y = 0; y < N; ++y) {
if (state[i][j] == goal[x][y]) {
distance += abs(i - x) + abs(j - y);
}
}
}
}
}
}
return distance;
}
//possible neighboring states
vector<vector<vector<int>>> getNeighbors(vector<vector<int>>& state) {
vector<vector<vector<int>>> neighbors;
int x, y;
// Find the position of the empty space (0)
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (state[i][j] == 0) {
x = i;
y = j;
break;
}
}
}
vector<pair<int, int>> moves = {{x-1, y}, {x+1, y}, {x, y-1}, {x, y+1}};
for (auto& move : moves) {
int newX = move.first, newY = move.second;
if (newX >= 0 && newX < N && newY >= 0 && newY < N) {
vector<vector<int>> newState = state;
swap(newState[x][y], newState[newX][newY]);
neighbors.push_back(newState);
}
}
return neighbors;
}
// Hill Climbing algorithm
vector<vector<int>> hillClimbing(vector<vector<int>>& start, vector<vector<int>>& goal) {
vector<vector<int>> currentState = start;
int currentCost = manhattanDistance(currentState, goal);
while (true) {
vector<vector<vector<int>>> neighbors = getNeighbors(currentState);
vector<vector<int>> nextState;
int nextCost = currentCost;
// Explore all neighbors and find the one with the least cost
for (auto& neighbor : neighbors) {
int neighborCost = manhattanDistance(neighbor, goal);
if (neighborCost < nextCost) {
nextState = neighbor;
nextCost = neighborCost;
}
}
// If no better neighbor is found, return the current state
if (nextCost >= currentCost) {
return currentState;
}
currentState = nextState;
currentCost = nextCost;
}
}
// Print the state of the puzzle
void printPuzzle(vector<vector<int>>& state) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cout << state[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int main() {
// Initial and goal state for the 8-puzzle problem
vector<vector<int>> startState = {
{1, 2, 3},
{4, 0, 5},
{7, 8, 6}
};
vector<vector<int>> goalState = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
cout << "Initial state:" << endl;
printPuzzle(startState);
// Run the hill climbing algorithm
vector<vector<int>> resultState = hillClimbing(startState, goalState);
cout << "Result state:" << endl;
printPuzzle(resultState);
return 0;
}