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
|
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <math.h>
#include <climits>
#include <cassert>
#include <chrono>
// #include "graph.h"
using namespace std;
using namespace chrono;
int I, N = 0;
struct Node {
vector<int> board;
string change;
int zero;
int currentDepth;
int remainingDepth;
Node() : board(), change(), zero(), currentDepth(), remainingDepth() {}
void update_zero() {
for (this->zero = 0; this->zero < this->board.size(); this->zero++) {
if (this->board[this->zero] == 0) return;
}
}
void update_remainingDepth() {
this->remainingDepth = this->manhattan_dist();
}
bool solved() {
return this->remainingDepth == 0;
}
int depth() {
return this->currentDepth + this->remainingDepth;
}
bool equals_any_in(vector<Node*>& arr) {
for (int i = 0; i < arr.size(); i++) {
if (*arr[i] == *this) {
return true;
}
}
return false;
}
void increment(int new_zero) {
swap(this->board[new_zero], this->board[this->zero]);
this->zero = new_zero;
this->currentDepth++;
this->update_remainingDepth();
}
private:
pair<int, int> to_2D(int i) {
return { i / N, i % N };
}
int manhattan_dist() {
int total = 0;
for (int i = 0; i < this->board.size(); i++) {
if (this->board[i] == 0) continue;
auto curr = to_2D(i);
auto goal = to_2D(this->board[i] - 1 + (this->board[i] > I));
total += abs(curr.first - goal.first) + abs(curr.second - goal.second);
}
return total;
}
public:
Node& operator=(const Node& other) {
if (this != &other) {
this->change = other.change;
this->board = other.board;
this->zero = other.zero;
this->currentDepth = other.currentDepth;
this->remainingDepth = other.remainingDepth;
}
return *this;
}
friend bool operator==(const Node& left, const Node& right) {
return left.board == right.board;
}
};
#define SOLVED -1
#define SKIP_J_IF(condition) if (condition) { delete j; continue; }
int dfs(vector<Node*>& path, int maxDepth) {
Node* i = path.back();
if (i->solved()) return SOLVED;
if (i->depth() > maxDepth) return i->depth();
int min = INT_MAX;
for (int dir = 0; dir < 4; dir++) {
Node* j = new Node(*i);
int new_zero = 0;
switch (dir) {
case 0: // left
SKIP_J_IF(j->zero % N == 0);
new_zero = j->zero - 1;
j->change = "right";
break;
case 1: // right
SKIP_J_IF(j->zero % N == N - 1);
new_zero = j->zero + 1;
j->change = "left";
break;
case 2: // up
SKIP_J_IF(j->zero / N == 0);
new_zero = j->zero - N;
j->change = "down";
break;
case 3: // down
SKIP_J_IF(j->zero / N == N - 1);
new_zero = j->zero + N;
j->change = "up";
break;
}
j->increment(new_zero);
// Don't go back a step
SKIP_J_IF(j->equals_any_in(path));
path.push_back(j);
int val = dfs(path, maxDepth);
if (val == SOLVED) return SOLVED;
if (val < min) min = val;
path.pop_back();
delete j;
}
return min;
}
bool solvable(Node* root) {
vector<int> &board = root->board;
int inversions = 0;
for (int i = 0; i < board.size(); i++) {
if (board[i] == 0) continue;
for (int j = i; j < board.size(); j++) {
if (board[j] == 0) continue;
inversions += board[i] > board[j];
}
}
// n is even, an n-by-n board is solvable
// iff the number of inversions + the row of the blank square is odd
if (board.size() % 2 == 0) {
inversions += root->zero / N;
return (inversions % 2) == ((I / N) % 2);
}
// when n is odd, an n-by-n board is solvable
// iff its number of inversions is even
else {
return inversions % 2 == 0;
}
}
int main(int argc, char* argv[]) {
/* Input */
cin >> N >> I;
if (I == -1) { I = N; }
N++;
Node* root = new Node;
int temp;
for (int i = 0; i < N; i++) {
cin >> temp;
root->board.push_back(temp);
}
N = sqrt(N + 1);
/* Prepare */
root->update_zero();
if (!solvable(root)) {
std::cout << -1 << std::endl;
delete root;
return 0;
}
root->update_remainingDepth();
vector<Node*> path;
path.push_back(root);
/* IDA* */
auto startTime = system_clock::now();
int bound = 3;
while(bound != SOLVED) {
bound = dfs(path, bound);
}
auto endTime = system_clock::now();
/* Output */
cout << (path.size() - 1) << endl;
for (int i = 1; i < path.size(); i++) {
cout << path[i]->change << endl;
delete path[i];
}
delete root;
if (argc > 1) {
auto millis = duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
std::cout << "Took: " << millis / 1000.0 << "s" << std::endl;
}
}
|