-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy path10181.cpp
200 lines (155 loc) · 4.6 KB
/
10181.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
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
int field[4][4];
bool solution_found;
vector<char> steps;
vector<char> solution_steps;
pair<int,int> find_pos(int field[4][4], int c) {
rep (i, 4)
rep (j, 4)
if (field[i][j] == c)
return make_pair(i,j);
}
int nr_of_inversions(int f[16]) {
int result = 0;
rep (i, 16) {
REP (j, i+1, 16) {
if (f[i] == 0 || f[j] == 0)
continue;
if (f[i] > f[j])
result++;
}
}
return result;
}
bool is_solvable() {
int f[16];
rep (i, 4)
rep (j, 4)
f[i*4+j] = field[i][j];
pair<int,int> blank = find_pos(field, 0);
int count = nr_of_inversions(f);
if ((4 - blank.first) % 2 == 0) {
return count % 2 == 1;
} else {
return count % 2 == 0;
}
}
int heuristic(int field[4][4]) {
static int f[4][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,0}};
int result = 0;
REP (i, 1, 16) {
pair<int,int> pos1 = find_pos(field, i);
pair<int,int> pos2 = find_pos(f, i);
result += abs(pos1.first - pos2.first);
result += abs(pos1.second - pos2.second);
}
return result;
}
bool is_goal_reached() {
static int f[4][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,0}};
rep (i, 4)
rep (j, 4)
if (field[i][j] != f[i][j])
return false;
return true;
}
void find_solution(int limit, char prev_move) {
if (solution_found || is_goal_reached()) {
solution_found = true;
solution_steps = steps;
return;
}
pair<int,int> blank = find_pos(field, 0);
/*
cout << "Current field:" << endl;
rep (i, 4) {
rep (j, 4) {
cout << field[i][j] << " ";
}
cout << endl;
}
cout << "Steps taken to reach this field: ";
rep (i, steps.size())
cout << steps[i];
cout << endl;
cout << "f(s) = h(s) + g(s) = " << heuristic(field) + steps.size() << endl;
cout << "current limit = " << limit << endl;
*/
// up
if (blank.first > 0 && prev_move != 'D') {
swap(field[blank.first][blank.second], field[blank.first-1][blank.second]);
int h = heuristic(field);
if (h + steps.size() + 1 <= limit && !solution_found) {
steps.push_back('U');
find_solution(limit, 'U');
steps.pop_back();
}
swap(field[blank.first][blank.second], field[blank.first-1][blank.second]);
}
// down
if (blank.first < 3 && prev_move != 'U') {
swap(field[blank.first][blank.second], field[blank.first+1][blank.second]);
int h = heuristic(field);
if (h + steps.size() + 1 <= limit && !solution_found) {
steps.push_back('D');
find_solution(limit, 'D');
steps.pop_back();
}
swap(field[blank.first][blank.second], field[blank.first+1][blank.second]);
}
// left
if (blank.second > 0 && prev_move != 'R') {
swap(field[blank.first][blank.second], field[blank.first][blank.second-1]);
int h = heuristic(field);
if (h + steps.size() + 1 <= limit && !solution_found) {
steps.push_back('L');
find_solution(limit, 'L');
steps.pop_back();
}
swap(field[blank.first][blank.second], field[blank.first][blank.second-1]);
}
// right
if (blank.second < 3 && prev_move != 'L') {
swap(field[blank.first][blank.second], field[blank.first][blank.second+1]);
int h = heuristic(field);
if (h + steps.size() + 1 <= limit && !solution_found) {
steps.push_back('R');
find_solution(limit, 'R');
steps.pop_back();
}
swap(field[blank.first][blank.second], field[blank.first][blank.second+1]);
}
}
int main(void) {
int n;
cin >> n;
while (n--) {
rep (i, 4)
rep (j, 4)
cin >> field[i][j];
if (!is_solvable()) {
cout << "This puzzle is not solvable." << endl;
continue;
}
int h = heuristic(field);
solution_found = false;
steps.clear();
while (true) {
find_solution(h, ' ');
if (solution_found) {
rep (i, solution_steps.size())
cout << solution_steps[i];
cout << endl;
break;
} else {
h += 5;
}
}
}
return 0;
}