forked from chenshuo/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuarong.cc
289 lines (251 loc) · 5.95 KB
/
huarong.cc
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
#include <boost/functional/hash/hash.hpp>
#include <deque>
#include <type_traits>
#include <unordered_set>
#include <vector>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
int board[5][4] = {
// 0 1 2 3
{ 1, 2, 2, 3, }, // 0
{ 1, 2, 2, 3, }, // 1
{ 4, 5, 5, 6, }, // 2
{ 4, 7, 8, 6, }, // 3
{ 9, 0, 0, 10 } }; // 4
struct Mask;
const int kRows = 5;
const int kColumns = 4;
const int kBlocks = 10;
enum class Shape // : int8_t
{
kInvalid,
kSingle,
kHorizon,
kVertical,
kSquare,
};
struct Block
{
Shape shape;
int left, top; // int8_t
Block()
: shape(Shape::kInvalid),
left(-1),
top(-1)
{
}
Block(Shape s, int left, int top)
: shape(s),
left(left),
top(top)
{
assert(shape != Shape::kInvalid);
assert(left >= 0 && left < kColumns);
assert(top >= 0 && top < kRows);
}
int bottom() const
{
const static int delta[] = { 0, 0, 0, 1, 1, };
assert(shape != Shape::kInvalid);
return top + delta[static_cast<int>(shape)];
}
int right() const
{
const static int delta[] = { 0, 0, 1, 0, 1, };
assert(shape != Shape::kInvalid);
return left + delta[static_cast<int>(shape)];
}
void mask(int8_t value, Mask* mask) const;
};
struct Mask
{
Mask()
{
bzero(board_, sizeof(board_));
}
bool operator==(const Mask& rhs) const
{
return memcmp(board_, rhs.board_, sizeof board_) == 0;
}
size_t hashValue() const
{
const int8_t* begin = board_[0];
return boost::hash_range(begin, begin + sizeof(board_));
}
void print() const
{
for (int i = 0; i < kRows; ++i)
{
for (int j = 0; j < kColumns; ++j)
{
printf(" %c", board_[i][j] + '0');
}
printf("\n");
}
}
void set(int8_t value, int y, int x)
{
assert(value > 0);
assert(x >= 0 && x < kColumns);
assert(y >= 0 && y < kRows);
assert(board_[y][x] == 0);
board_[y][x] = value;
}
bool empty(int y, int x) const
{
assert(x >= 0 && x < kColumns);
assert(y >= 0 && y < kRows);
return board_[y][x] == 0;
}
private:
int8_t board_[kRows][kColumns];
};
namespace std
{
template<> struct hash<Mask>
{
size_t operator()(const Mask& x) const
{
return x.hashValue();
}
};
}
inline void Block::mask(int8_t value, Mask* mask) const
{
mask->set(value, top, left);
switch (shape)
{
case Shape::kHorizon:
mask->set(value, top, left+1);
break;
case Shape::kVertical:
mask->set(value, top+1, left);
break;
case Shape::kSquare:
mask->set(value, top, left+1);
mask->set(value, top+1, left);
mask->set(value, top+1, left+1);
break;
default:
assert(shape == Shape::kSingle);
;
}
}
struct State
{
Mask toMask() const
{
Mask m;
for (int i = 0; i < kBlocks; ++i)
{
Block b = blocks_[i];
b.mask(static_cast<int>(b.shape), &m);
}
return m;
}
bool isSolved() const
{
// FIXME: magic number
Block square = blocks_[1];
assert(square.shape == Shape::kSquare);
return (square.left == 1 && square.top == 3);
}
template<typename FUNC>
void move(const FUNC& func) const
{
static_assert(std::is_convertible<FUNC, std::function<void(const State&)>>::value,
"func must be callable with a 'const State&' parameter.");
const Mask mask = toMask();
for (int i = 0; i < kBlocks; ++i)
{
Block b = blocks_[i];
// move up
if (b.top > 0 && mask.empty(b.top-1, b.left)
&& mask.empty(b.top-1, b.right()))
{
State next = *this;
next.step++;
next.blocks_[i].top--;
func(next);
}
// move down
if (b.bottom() < kRows-1 && mask.empty(b.bottom()+1, b.left)
&& mask.empty(b.bottom()+1, b.right()))
{
State next = *this;
next.step++;
next.blocks_[i].top++;
func(next);
}
// move left
if (b.left > 0 && mask.empty(b.top, b.left-1)
&& mask.empty(b.bottom(), b.left-1))
{
State next = *this;
next.step++;
next.blocks_[i].left--;
func(next);
}
// move right
if (b.right() < kColumns-1 && mask.empty(b.top, b.right()+1)
&& mask.empty(b.bottom(), b.right()+1))
{
State next = *this;
next.step++;
next.blocks_[i].left++;
func(next);
}
}
}
// std::vector<State> moves() const;
Block blocks_[kBlocks];
int step = 0;
};
int main()
{
printf("sizeof(Mask) = %zd, sizeof(State) = %zd\n", sizeof(Mask), sizeof(State));
std::unordered_set<Mask> seen;
std::deque<State> queue;
State initial;
initial.blocks_[0] = Block(Shape::kVertical, 0, 0);
initial.blocks_[1] = Block(Shape::kSquare, 1, 0);
initial.blocks_[2] = Block(Shape::kVertical, 3, 0);
initial.blocks_[3] = Block(Shape::kVertical, 0, 2);
initial.blocks_[4] = Block(Shape::kHorizon, 1, 2);
initial.blocks_[5] = Block(Shape::kVertical, 3, 2);
initial.blocks_[6] = Block(Shape::kSingle, 1, 3);
initial.blocks_[7] = Block(Shape::kSingle, 2, 3);
initial.blocks_[8] = Block(Shape::kSingle, 0, 4);
initial.blocks_[9] = Block(Shape::kSingle, 3, 4);
queue.push_back(initial);
seen.insert(initial.toMask());
while (!queue.empty())
{
const State curr = queue.front();
queue.pop_front();
if (curr.isSolved())
{
printf("found solution with %d steps\n", curr.step);
break;
}
else if (curr.step > 200)
{
printf("too many steps.\n");
break;
}
curr.move([&seen, &queue](const State& next) {
auto result = seen.insert(next.toMask());
if (result.second)
queue.push_back(next);
});
// for (const State& next : curr.moves())
// {
// auto result = seen.insert(next.toMask());
// if (result.second)
// queue.push_back(next);
// }
}
}