-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment-7.cpp
195 lines (162 loc) · 5.74 KB
/
Assignment-7.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>
#include <cmath>
#include <chrono>
#include <algorithm>
using namespace std;
const int N = 3;
struct State {
vector<vector<int>> board;
int g, h, f;
pair<int, int> blank;
vector<State*> path;
State(vector<vector<int>> b, int g, int h, pair<int, int> blank, vector<State*> path)
: board(b), g(g), h(h), f(g + h), blank(blank), path(path) {}
bool operator<(const State& other) const {
return f > other.f;
}
};
int calculateMisplacedTiles(const vector<vector<int>>& board, const vector<vector<int>>& goal) {
int count = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] != 0 && board[i][j] != goal[i][j]) {
count++;
}
}
}
return count;
}
int calculateManhattanDistance(const vector<vector<int>>& board, const vector<vector<int>>& goal) {
int distance = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] != 0) {
for (int x = 0; x < N; x++) {
for (int y = 0; y < N; y++) {
if (board[i][j] == goal[x][y]) {
distance += abs(i - x) + abs(j - y);
}
}
}
}
}
}
return distance;
}
vector<State*> getNeighbors(State* current) {
vector<State*> neighbors;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
for (int i = 0; i < 4; i++) {
int nx = current->blank.first + dx[i];
int ny = current->blank.second + dy[i];
if (nx >= 0 && nx < N && ny >= 0 && ny < N) {
vector<vector<int>> newBoard = current->board;
swap(newBoard[current->blank.first][current->blank.second], newBoard[nx][ny]);
vector<State*> newPath = current->path;
newPath.push_back(current);
neighbors.push_back(new State(newBoard, current->g + 1, 0, make_pair(nx, ny), newPath));
}
}
return neighbors;
}
string boardToString(const vector<vector<int>>& board) {
string result;
for (const auto& row : board) {
for (int val : row) {
result += to_string(val);
}
}
return result;
}
vector<State*> solvePuzzle(const vector<vector<int>>& initial, const vector<vector<int>>& goal, bool useManhattan) {
auto cmp = [](State* a, State* b) { return a->f > b->f; };
priority_queue<State*, vector<State*>, decltype(cmp)> openList(cmp);
unordered_set<string> closedList;
pair<int, int> initialBlank;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (initial[i][j] == 0) {
initialBlank = make_pair(i, j);
break;
}
}
}
State* start = new State(initial, 0, 0, initialBlank, {});
start->h = useManhattan ? calculateManhattanDistance(initial, goal) : calculateMisplacedTiles(initial, goal);
openList.push(start);
while (!openList.empty()) {
State* current = openList.top();
openList.pop();
if (current->board == goal) {
return current->path;
}
string boardStr = boardToString(current->board);
if (closedList.find(boardStr) != closedList.end()) {
continue;
}
closedList.insert(boardStr);
for (State* neighbor : getNeighbors(current)) {
neighbor->h = useManhattan ? calculateManhattanDistance(neighbor->board, goal) : calculateMisplacedTiles(neighbor->board, goal);
openList.push(neighbor);
}
}
return {};
}
int main() {
int x, y;
vector<vector<int>> initial(N, vector<int>(N));
int inversions = 0;
cout << "Enter the Initial State" << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> initial[i][j];
if (initial[i][j] == 0) {
x = i;
y = j;
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
for (int l = 0; l < N; l++) {
if ((i < k) || (i == k && j < l)) {
if (initial[i][j] > initial[k][l] && initial[k][l] != 0) {
inversions++;
}
}
}
}
}
}
vector<vector<int>> goal = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
if (inversions % 2 != 0) {
cout << "Not Possible!" << endl;
} else {
cout << "Solving with Misplaced Tiles heuristic..." << endl;
auto start = chrono::high_resolution_clock::now();
vector<State*> solutionMisplaced = solvePuzzle(initial, goal, false);
auto end = chrono::high_resolution_clock::now();
auto durationMisplaced = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "Misplaced Tiles:" << endl;
cout << "Time taken: " << durationMisplaced.count() << " milliseconds" << endl;
cout << "Number of moves: " << solutionMisplaced.size() << endl;
cout << "Solving with Manhattan Distance heuristic..." << endl;
start = chrono::high_resolution_clock::now();
vector<State*> solutionManhattan = solvePuzzle(initial, goal, true);
end = chrono::high_resolution_clock::now();
auto durationManhattan = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "\nManhattan Distance:" << endl;
cout << "Time taken: " << durationManhattan.count() << " milliseconds" << endl;
cout << "Number of moves: " << solutionManhattan.size() << endl;
}
return 0;
}