-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
281 lines (248 loc) · 7.68 KB
/
Source.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
/*
Npuzzle solver
even though it assumes you'll enter 4x4 puzzle (15 puzzle),
you achieve others by changing the loop in main func.
and also changing goal state to your exact needs
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <ctime>
using namespace std;
struct node {
vector<vector<int>> puzzle;
int cost;
int depth;
node* right;
node* left;
node* up;
node* down;
node* parent;
};
void display_puzzle(vector<vector<int>> p) {
for (int i = 0; i < p.size(); i++) {
for (int j = 0; j < p.size(); j++) {
cout << p[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
class Npuzzle {
public:
Npuzzle() {
start_state = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
root = new node;
goal_state = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
//goal_state = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 0 } };
root = new node;
root->puzzle = start_state;
root->parent = NULL;
root->cost = cost_puzzle(start_state);
root->depth = 0;
}
Npuzzle(vector<vector<int>> p) {
start_state = p;
goal_state = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 0 } };
root = new node;
root->puzzle = start_state;
root->parent = NULL;
root->cost = cost_puzzle(start_state);
root->depth = 0;
}
void solve_puzzle() {
open_list.insert({ puzzle_to_string(root->puzzle), root });
pair < unordered_map<string, node*>::iterator, double> smallest;
while (!open_list.empty()) {
double weight_cost = 1;
double weight_depth = 0.1; // Keeping it low like this can help to move deeper and quickly solve the puzzle
smallest.first = open_list.begin();
smallest.second = weight_cost*smallest.first->second->cost + weight_depth*smallest.first->second->depth;
for (unordered_map<string, node*>::iterator it = open_list.begin(); it != open_list.end(); it++) {
if (smallest.second > weight_cost*it->second->cost + weight_depth*it->second->depth) {
smallest.first = it;
smallest.second = weight_cost*it->second->cost + weight_depth*it->second->depth;
}
}
current = (*(smallest.first)).second;
swap_puzzle();
std::cout << "Depth is: " << current->depth << std::endl;
while (solved) {
spuzzle.push_back(current->puzzle);
if (current->parent != NULL) {
current = current->parent;
}
else {
cout << "Number of puzzles generated: " << open_list.size() + closed_list.size() << endl;
goto endloop;
}
}
closed_list.insert({ puzzle_to_string(current->puzzle), current });
open_list.erase(puzzle_to_string(current->puzzle));
//cout << current->depth << " ---- " << open_list.size() + closed_list.size() << " ---- " << current->cost << endl;
//cout << "Number of puzzles generated: " << open_list.size() + closed_list.size() << endl;
//cout << open_list.size() << endl;
}
endloop:;
}
vector<vector<vector<int>>> get_spuzzle() {
return spuzzle;
}
bool contains_puzzle(vector<vector<int>> &p) {
//unordered_map<string, node*>::const_iterator got = closed_list.find(puzzle_to_string(p));
if (closed_list.count(puzzle_to_string(p)) > 0) {
return false;
}
return true;
}
void swap_puzzle() {
for (int i = 0; i < current->puzzle.size(); i++) {
for (int j = 0; j < current->puzzle.size(); j++) {
if (current->puzzle[i][j] == 0) {
if (j + 1 < current->puzzle.size()) {
vector<vector<int>> temp = current->puzzle;
swap(temp[i][j + 1], temp[i][j]);
if (temp == goal_state) {
spuzzle.push_back(temp);
solved = true;
cout << "Solved" << endl;
}
else if (contains_puzzle(temp)) {
current->right = new node;
current->right->depth = current->depth + 1;
current->right->parent = current;
current->right->puzzle = temp;
open_list.insert({ puzzle_to_string(temp), current->right });
}
}
//Left
if (j - 1 >= 0) {
vector<vector<int>> temp = current->puzzle;
swap(temp[i][j - 1], temp[i][j]);
if (temp == goal_state) {
spuzzle.push_back(temp);
solved = true;
cout << "solved" << endl;
}
else if (contains_puzzle(temp)) {
current->left = new node;
current->left->cost = cost_puzzle(temp);
current->left->depth = current->depth + 1;
current->left->parent = current;
current->left->puzzle = temp;
open_list.insert({ puzzle_to_string(temp), current->left });
}
}
//Up
if (i - 1 >= 0) {
vector<vector<int>> temp = current->puzzle;
swap(temp[i - 1][j], temp[i][j]);
if (temp == goal_state) {
spuzzle.push_back(temp);
solved = true;
cout << "solved" << endl;
}
else if (contains_puzzle(temp)) {
current->up = new node;
current->up->cost = cost_puzzle(temp);
current->up->depth = current->depth + 1;
current->up->parent = current;
current->up->puzzle = temp;
open_list.insert({ puzzle_to_string(temp), current->up });
}
}
//Down
if (i + 1 < current->puzzle.size()) {
vector<vector<int>> temp = current->puzzle;
swap(temp[i + 1][j], temp[i][j]);
if (temp == goal_state) {
spuzzle.push_back(temp);
solved = true;
cout << "solved" << endl;
}
else if (contains_puzzle(temp)) {
current->down = new node;
current->down->cost = cost_puzzle(temp);
current->down->depth = current->depth + 1;
current->down->parent = current;
current->down->puzzle = temp;
open_list.insert({ puzzle_to_string(temp), current->down });
}
}
}
}
}
}
int cost_puzzle(vector<vector<int>> p) {
int cost = 0;
for (int i = 0; i < p.size(); i++) {
for (int j = 0; j < p.size(); j++) {
if (p[i][j] != 0) {
if (p[i][j] % p.size() != 0) {
int temp_i = p[i][j] / p.size();
int temp_j = p[i][j] % p.size() - 1;
cost = absolute(temp_i - i) + absolute(temp_j - j) + cost;
}
else {
int temp_i = p[i][j] / p.size() - 1;
int temp_j = p.size() - 1;
cost = absolute(temp_i - i) + absolute(temp_j - j) + cost;
}
}
else {
cost = absolute(p.size() - 1 - i) + absolute(p.size() - 1 - j) + cost;
}
}
}
return cost;
}
int absolute(int val) {
if (val >= 0) {
return val;
}
else {
return -val;
}
}
string puzzle_to_string(vector<vector<int>> p) {
string str;
for (int i = 0; i < p.size(); i++) {
for (int j = 0; j < p.size(); j++) {
str += to_string(p[i][j]);
}
}
return str;
}
private:
unordered_map<string, node*> open_list;
unordered_map<string, node*> closed_list;
vector<vector<int>> start_state;
vector<vector<int>> goal_state;
vector<vector<vector<int>>> spuzzle;
node* current;
node* root;
bool solved = false;
};
int main() {
vector<vector<int>> start_puzzle = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 0 } };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << "Enter the " << i * 4 + j + 1 << ". number: ";
cin >> start_puzzle[i][j];
cout << endl;
}
}
int start_s = clock();
Npuzzle init(start_puzzle);
init.solve_puzzle();
vector<vector<vector<int>>> spuzzle = init.get_spuzzle();
for (int i = 0; i < spuzzle.size(); i++) {
display_puzzle(spuzzle[i]);
}
cout << "Steps: " << spuzzle.size() << endl;
cout << "the end" << endl;
int stop_s = clock();
cout << "time: " << (stop_s - start_s) / double(CLOCKS_PER_SEC) << endl;
}