-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve_npuzzle.cpp
384 lines (356 loc) · 10.6 KB
/
solve_npuzzle.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <fstream>
using namespace std;
class Board
{
private:
int dimension;
vector<vector<int>> grid;
Board *parent;
public:
Board();
int get_dimension();
vector<vector<int>> get_grid();
Board *get_parent();
Board *set_dimension(int _dimension);
Board *set_grid(vector<vector<int>> _grid);
Board *set_parent(Board *_parent);
vector<int> get_linear_grid();
int get_number_of_inversions();
int get_blank_row_number(bool _from_last = true);
bool is_solvable();
int get_hamming_distance();
int get_manhattan_distance();
bool has_same_grid_as(vector<vector<int>> _other_grid);
pair<int, int> get_blank_indices();
vector<Board *> get_reachable_boards();
bool is_goal_board();
friend istream &operator>>(istream &_stream, Board &_board);
friend ostream &operator<<(ostream &_stream, Board &_board);
};
Board::Board()
{
this->parent = nullptr;
}
int Board::get_dimension()
{
return this->dimension;
}
vector<vector<int>> Board::get_grid()
{
return this->grid;
}
Board *Board::get_parent()
{
return this->parent;
}
Board *Board::set_dimension(int _dimension)
{
this->dimension = _dimension;
return this;
}
Board *Board::set_grid(vector<vector<int>> _grid)
{
this->grid = _grid;
return this;
}
Board *Board::set_parent(Board *_parent)
{
this->parent = _parent;
return this;
}
vector<int> Board::get_linear_grid()
{
vector<int> linear_grid;
for (int i = 0; i < this->dimension; i++)
{
for (int j = 0; j < this->dimension; j++)
{
linear_grid.push_back(this->grid[i][j]);
}
}
return linear_grid;
}
int Board::get_number_of_inversions()
{
vector<int> linear_grid = this->get_linear_grid();
int number_of_inversions = 0, linear_grid_size = linear_grid.size();
for (int i = 0; i < linear_grid_size; i++)
{
for (int j = i + 1; j < linear_grid_size; j++)
{
if (linear_grid[i] > 0 && linear_grid[j] > 0)
{
number_of_inversions += (linear_grid[j] < linear_grid[i]);
}
}
}
return number_of_inversions;
}
int Board::get_blank_row_number(bool _from_last)
{
int blank_row;
for (int i = 0; i < this->dimension; i++)
{
for (int j = 0; j < this->dimension; j++)
{
if (this->grid[i][j] == 0)
{
blank_row = i + 1;
break;
}
}
}
if (_from_last)
{
blank_row = this->dimension + 1 - blank_row;
}
return blank_row;
}
bool Board::is_solvable()
{
if (this->dimension & 1)
{
return (this->get_number_of_inversions() % 2 == 0);
}
else
{
if (((this->get_blank_row_number() % 2 == 0) && (this->get_number_of_inversions() & 1)) || ((this->get_blank_row_number() & 1) && (this->get_number_of_inversions() % 2 == 0)))
{
return true;
}
return false;
}
}
int Board::get_hamming_distance()
{
int hamming_distance = 0;
vector<int> linear_grid = this->get_linear_grid();
for (int i = 0; i < linear_grid.size(); i++)
{
hamming_distance += (linear_grid[i] > 0 && linear_grid[i] != i + 1);
}
return hamming_distance;
}
int Board::get_manhattan_distance()
{
int manhattan_distance = 0;
for (int i = 0; i < this->dimension; i++)
{
for (int j = 0; j < this->dimension; j++)
{
if (this->grid[i][j] > 0)
{
int value = this->grid[i][j] - 1;
int expected_row = value / this->dimension;
int expected_col = value % this->dimension;
manhattan_distance += (abs(expected_row - i) + abs(expected_col - j));
}
}
}
return manhattan_distance;
}
bool Board::has_same_grid_as(vector<vector<int>> _other_grid)
{
if (_other_grid.size() != _other_grid[0].size() || this->dimension != _other_grid[0].size())
{
return false;
}
for (int i = 0; i < this->dimension; i++)
{
for (int j = 0; j < this->dimension; j++)
{
if (this->grid[i][j] != _other_grid[i][j])
{
return false;
}
}
}
return true;
}
pair<int, int> Board::get_blank_indices()
{
pair<int, int> indices = {-1, -1};
for (int i = 0; i < this->dimension; i++)
{
for (int j = 0; j < this->dimension; j++)
{
if (this->grid[i][j] == 0)
{
indices = {i, j};
}
}
}
return indices;
}
vector<Board *> Board::get_reachable_boards()
{
vector<Board *> reachable_boards;
vector<pair<int, int>> delta_xy = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
pair<int, int> blank_indices = this->get_blank_indices();
for (auto dxy : delta_xy)
{
pair<int, int> new_xy = {blank_indices.first + dxy.first, blank_indices.second + dxy.second};
if (new_xy.first < 0 || new_xy.first >= this->dimension || new_xy.second < 0 || new_xy.second >= this->dimension)
{
continue;
}
auto new_grid = this->get_grid();
swap(new_grid[blank_indices.first][blank_indices.second], new_grid[new_xy.first][new_xy.second]);
if (this->get_parent() != nullptr && this->get_parent()->has_same_grid_as(new_grid))
{
continue;
}
Board *new_board = new Board();
new_board->set_dimension(this->get_dimension());
new_board->set_grid(new_grid);
new_board->set_parent(this);
reachable_boards.push_back(new_board);
}
return reachable_boards;
}
bool Board::is_goal_board()
{
for (int i = 0; i < this->dimension; i++)
{
for (int j = 0; j < this->dimension; j++)
{
if (this->grid[i][j] > 0 && this->grid[i][j] != i * this->dimension + j + 1)
{
return false;
}
}
}
return true;
}
istream &operator>>(istream &_stream, Board &_board)
{
_stream >> _board.dimension;
_board.grid.assign(_board.dimension, vector<int>(_board.dimension));
for (int i = 0; i < _board.dimension; i++)
{
for (int j = 0; j < _board.dimension; j++)
{
_stream >> _board.grid[i][j];
}
}
return _stream;
}
ostream &operator<<(ostream &_stream, Board &_board)
{
_stream << "\n";
for (int i = 0; i < _board.dimension; i++)
{
for (int j = 0; j < _board.dimension; j++)
{
_stream << _board.grid[i][j] << " \n"[j == _board.dimension - 1];
}
}
return _stream;
}
void solve_npuzzle_hamming(Board *_initial_board, ostream &_cout = cout)
{
if (!_initial_board->is_solvable())
{
_cout << "Unsolvable puzzle\n";
}
else
{
priority_queue<pair<int, Board *>, vector<pair<int, Board *>>, greater<pair<int, Board *>>> hamming_queue;
int number_of_moves = 0, number_of_explored_boards = 0, number_of_expanded_boards = 0;
Board *goal_board = nullptr;
hamming_queue.push({_initial_board->get_hamming_distance(), _initial_board});
while (!hamming_queue.empty())
{
Board *current_board = hamming_queue.top().second;
int simple_distance = hamming_queue.top().first - current_board->get_hamming_distance();
hamming_queue.pop();
if (current_board->is_goal_board())
{
goal_board = current_board;
break;
}
number_of_expanded_boards++;
auto reachable_boards = current_board->get_reachable_boards();
for (auto reachable_board : reachable_boards)
{
hamming_queue.push({simple_distance + 1 + reachable_board->get_hamming_distance(), reachable_board});
number_of_explored_boards++;
}
}
Board *current_move = goal_board;
stack<Board *> moves;
while (current_move != nullptr)
{
moves.push(current_move);
current_move = current_move->get_parent();
}
number_of_moves = int(moves.size()) - 1;
_cout << "\nMinimum number of moves: " << number_of_moves << "\nNumber of explored boards: " << number_of_explored_boards << "\nNumber of expanded boards: " << number_of_expanded_boards << "\n";
while (!moves.empty())
{
_cout << *moves.top();
moves.pop();
}
}
}
void solve_npuzzle_manhattan(Board *_initial_board, ostream &_cout = cout)
{
if (!_initial_board->is_solvable())
{
_cout << "Unsolvable puzzle\n";
}
else
{
priority_queue<pair<int, Board *>, vector<pair<int, Board *>>, greater<pair<int, Board *>>> manhattan_queue;
int number_of_moves = 0, number_of_explored_boards = 0, number_of_expanded_boards = 0;
Board *goal_board = nullptr;
manhattan_queue.push({_initial_board->get_manhattan_distance(), _initial_board});
while (!manhattan_queue.empty())
{
Board *current_board = manhattan_queue.top().second;
int simple_distance = manhattan_queue.top().first - current_board->get_manhattan_distance();
manhattan_queue.pop();
if (current_board->is_goal_board())
{
goal_board = current_board;
break;
}
number_of_expanded_boards++;
auto reachable_boards = current_board->get_reachable_boards();
for (auto reachable_board : reachable_boards)
{
manhattan_queue.push({simple_distance + 1 + reachable_board->get_manhattan_distance(), reachable_board});
number_of_explored_boards++;
}
}
Board *current_move = goal_board;
stack<Board *> moves;
while (current_move != nullptr)
{
moves.push(current_move);
current_move = current_move->get_parent();
}
number_of_moves = int(moves.size()) - 1;
_cout << "\nMinimum number of moves: " << number_of_moves << "\nNumber of explored boards: " << number_of_explored_boards << "\nNumber of expanded boards: " << number_of_expanded_boards << "\n";
// _cout << "\nMinimum number of moves: " << number_of_moves << "\n";
while (!moves.empty())
{
_cout << *moves.top();
moves.pop();
}
}
}
int main()
{
ifstream in("input.txt");
ofstream out("output.txt");
Board *initial_board = new Board();
cin >> (*initial_board);
// solve_npuzzle_hamming(initial_board);
solve_npuzzle_manhattan(initial_board);
return 0;
}