-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-search.cpp
211 lines (179 loc) · 4.45 KB
/
basic-search.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
// Gabriel R. S. Pupo - nUSP: 9896250
// THE FOLLOWING CODE USES BREADTH-FIRST SEARCH TO SOLVE 15-PUZZLES.
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <ctime>
using namespace std;
struct coord {
int i, j;
};
struct state {
int puzzle[4][4];
state *prev;
char move;
};
int count_inversions(int puzzle[]) {
int counter = 0;
for (int i = 0; i < 15; i++)
for (int j = i+1; j <= 15; j++)
if (puzzle[j] && puzzle[i] && puzzle[i] > puzzle[j]) counter++;
counter++;
return counter;
}
bool is_solvable(int puzzle[4][4], coord zero) {
int inv = count_inversions((int *) puzzle);
return (zero.i % 2 == 0) ? inv % 2 == 0 : inv % 2 != 0;
}
int goal_puzzle[4][4] = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
int null_puzzle[4][4] = { {0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
void swap(int puzzle[4][4], coord c1, coord c2) {
int aux = puzzle[c1.i][c1.j];
puzzle[c1.i][c1.j] = puzzle[c2.i][c2.j];
puzzle[c2.i][c2.j] = aux;
}
bool cmp_2d(int a[4][4], int b[4][4]) {
int counter = 0;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
if (a[i][j] == b[i][j]) counter++;
if (counter == 16)
return true;
return false;
}
inline bool is_solved(int candidate[4][4]) {
return (cmp_2d(goal_puzzle, candidate));
}
coord find_blank(int puzzle[4][4]) {
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
if (puzzle[i][j] == 0)
return {i,j};
return {-1,-1};
}
vector<state*> get_possible_moves(state *cur) {
coord blank = find_blank(cur->puzzle);
vector<state*> possible;
// UP
if (blank.i-1 >= 0) {
state *up = new state;
memcpy(up->puzzle, cur->puzzle, 16*sizeof(int));
swap(up->puzzle, blank, { blank.i-1, blank.j });
up->move = 'U';
possible.push_back(up);
}
// DOWN
if (blank.i+1 < 4) {
state *down = new state;
memcpy(down->puzzle, cur->puzzle, 16*sizeof(int));
swap(down->puzzle, blank, { blank.i+1, blank.j });
down->move = 'D';
possible.push_back(down);
}
// LEFT
if (blank.j-1 >= 0) {
state *left = new state;
memcpy(left->puzzle, cur->puzzle, 16*sizeof(int));
swap(left->puzzle, blank, { blank.i, blank.j-1 });
left->move = 'L';
possible.push_back(left);
}
// RIGHT
if (blank.j+1 < 4) {
state *right = new state;
memcpy(right->puzzle, cur->puzzle, 16*sizeof(int));
swap(right->puzzle, blank, { blank.i, blank.j+1 });
right->move = 'R';
possible.push_back(right);
}
return possible;
}
bool was_visited(state *st, vector<state*> all) {
for (state *s : all)
if (cmp_2d(st->puzzle, s->puzzle)) return true;
return false;
}
bool solved = false;
vector<state*> all_states;
queue<state*> solver;
void delete_all() {
while (not solver.empty()) solver.pop();
while (not all_states.empty()) delete all_states.back(), all_states.pop_back();
}
void solve(int puzzle[4][4]) {
state *init = new state, *last;
memcpy(init->puzzle, puzzle, 16*sizeof(int));
init->prev = NULL;
init->move = 'o';
if (is_solved(init->puzzle)) {
delete init;
cout << "\n";
return;
}
solver.push(init);
all_states.push_back(init);
do {
state *current = solver.front();
vector<state*> next_moves = get_possible_moves(current);
for (state *move : next_moves) {
if (is_solved(move->puzzle)) {
move->prev = current;
last = move;
solved = true;
all_states.push_back(move);
break;
} else if (not was_visited(move, all_states)) {
move->prev = current;
solver.push(move);
all_states.push_back(move);
} else delete move;
}
solver.pop(); // current state is pop'd
} while (not solver.empty() and not solved);
string path;
while (last->prev != NULL) {
path += last->move;
last = last->prev;
}
reverse(path.begin(), path.end());
cout << path << "\n";
delete_all();
}
int main(void) {
int sets, puzzle[4][4] = {{0}};
double start_time, end_time;
coord zero;
cin >> sets;
for (int s = 0; s < sets; ++s) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
scanf("%d", &puzzle[i][j]);
if (puzzle[i][j] == 0) {
zero.i = i;
zero.j = j;
}
}
}
if (not is_solvable(puzzle, zero)) cout << "This puzzle is not solvable.\n";
else {
start_time = clock();
solve(puzzle);
end_time = clock();
cout << "\nExecution time for this set: " << (end_time-start_time)/CLOCKS_PER_SEC << " s\n\n";
memcpy(puzzle, null_puzzle, 16*sizeof(int));
solved = false;
}
}
return 0;
}