-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonogram_solver.cpp
599 lines (542 loc) · 15 KB
/
nonogram_solver.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include "nonogram_solver.h"
#include <iostream>
// Slice implementation
Slice::Slice(Solver &solver, int offset0, int step, int length)
: solver_(solver),
g_(solver.g_),
offset0_(offset0),
step_(step),
length_(length){};
Slice::Slice(Solver &solver, LineName name) : solver_(solver), g_(solver.g_) {
if (name.dir == Direction::ROW) {
offset0_ = solver.width_ * name.index;
step_ = 1;
length_ = solver.width_;
} else {
offset0_ = name.index;
step_ = solver.width_;
length_ = solver.height_;
}
}
void Slice::set(int i, CellState s) const {
int o = offset0_ + step_ * i;
int x = o % solver_.width_;
int y = (o - x) / solver_.width_;
solver_.set(x, y, s);
}
int Slice::findHoleStartingAt(int start, int length) const {
int found = 0;
for (int i = start; i < length_; i++) {
if (get(i) == CellState::CROSSED) {
found = 0;
} else {
found += 1;
if (found >= length) {
return i - found + 1;
}
}
}
return -1;
};
int Slice::stripLength(int i) const {
CellState val = get(i);
int n = 0;
for (; i < length_; i++) {
if (get(i) == val) {
n++;
} else {
return n;
}
}
return n;
}
int Slice::indexOfNextSolid(int start, int bound) const {
for (int i = start; i < bound; i++) {
if (get(i) == CellState::SOLID) {
return i;
}
}
return -1;
}
// setSegment between i and j (exclusive) to state val. Return number
// of cells changed.
int Slice::setSegment(int i, int j, CellState val) const {
int changed = 0;
for (int n = i; n < j; n++) {
if (get(n) != val) {
set(n, val);
changed++;
}
}
return changed;
}
Slice Slice::reverse() const {
return Slice(solver_, offset0_ + step_ * (length_ - 1), -step_, length_);
}
// Line implementation
Line::Line(Solver &solver, LineName name, std::vector<int> &&len)
: len_(len),
lb_(len.size()),
ub_(len.size()),
done_(len.size()),
slice_(solver, name),
name(name) {
int sum = 0;
for (auto l : len) {
sum += l;
}
stats.wiggleRoom = slice_.length() - sum;
stats.numSegments = len.size();
stats.doneSegments = 0;
stats.numChanges = 0;
}
void Line::updateStats() {
int w = 0;
for (int i = 0; i < lb_.size(); i++) {
int wi = ub(i) - lb(i) + 1 - len_[i];
if (w < wi) w = wi;
}
stats.wiggleRoom = w;
int total_done = 0;
for (auto d : done_) {
if (d) {
total_done++;
}
}
stats.doneSegments = total_done;
stats.numChanges = 0;
}
// Fit all segments to the leftmost position in slice, satisfying the
// constraints. Also obey known lower bound. Returns false when no fit
// can be found.
bool Line::fitLeftMost(Slice slice, const std::vector<int> &len,
std::vector<int> &lb) {
int cursor = 0; // cursor tracks a position in slice
int i = 0; // i is an index of sLen / lb
while (cursor < slice.length()) {
int lBound = i >= len.size() ? slice.length() : lb[i];
if (lBound > cursor) {
int nextSolid = slice.indexOfNextSolid(cursor, lBound);
if (nextSolid == -1) {
cursor = lBound;
continue;
}
// we need to pull a segment here to cover nextSolid.
int stripLen = slice.stripLength(nextSolid);
do {
i--;
} while (i >= 0 && len[i] < stripLen);
if (i < 0) {
return false;
}
// move cursor back to where the pulled segment was.
// Then pull the segment to the place where it'll
// cover nextSolid. Skip to next round because we may
// have exposed a SOLID strip when pulling segment i.
cursor = lb[i];
lb[i] = nextSolid + stripLen - len[i];
continue;
}
// see if we can find a hole at cursor that's big enough.
int hole = slice.findHoleStartingAt(cursor, len[i]);
if (hole == -1) {
return false;
}
// Move the segment forward if the tail is next to a solid
// cell. Also remember if skipped over solids - need to
// trace back and find an earlier segment to cover it in
// that case.
bool skippedSolid = false;
while (hole + len[i] < slice.length() &&
slice.get(hole + len[i]) == CellState::SOLID) {
skippedSolid = skippedSolid || slice.get(hole) == CellState::SOLID;
hole++;
}
lb[i] = hole;
if (!skippedSolid) {
// set next allowable position and work on next segment
cursor = hole + len[i] + 1;
i++;
}
} // cursor loop
// check remaining segment constraints
if (i < len.size()) {
return false;
}
return true;
}
bool Line::inferSegments() {
for (int i = 0; i < numSegments(); i++) {
int l = lb(i);
int u = ub(i);
int prevU = i > 0 ? ub(i - 1) : -1;
if (l + len(i) - 1 > u) {
return false;
}
if (l > prevU + 1) {
slice_.setSegment(prevU + 1, l, CellState::CROSSED);
}
if (done(i)) {
continue;
}
if (u - len(i) + 1 <= l + len(i) - 1) {
slice_.setSegment(u - len(i) + 1, l + len(i), CellState::SOLID);
}
if (u - l + 1 == len(i)) {
done_[i] = true;
}
}
if (ub(numSegments() - 1) + 1 < slice_.length()) {
slice_.setSegment(ub(numSegments() - 1) + 1, slice_.length(),
CellState::CROSSED);
}
return true;
}
// returns a segment index ranges (left inclusive, right exclusive)
// that lb(i) <= start and ub(i) >= end.
std::pair<int, int> Line::collidingSegments(int start, int end) {
int first = 0, second = 0;
bool found = false;
for (int i = 0; i < numSegments(); i++) {
if (ub(i) < end) {
continue;
}
if (lb(i) <= start) {
if (!found) {
found = true;
first = i;
}
second = i + 1;
} else if (found) {
break;
}
}
return std::make_pair(first, second);
}
// Make inference for strips (consecutive cells with same state).
// Cases include:
//
// 1. "X X" can be marked "XXX" if all possible segments >= 2
// 2. "?SSS?" can be marked "XSSSX" if all possible segments =3.
// 3. "X SS " can be marked "X SSS" if all potential segments >= 4.
bool Line::inferStrips() {
int stripLen = 0;
for (int i = 0; i < slice_.length(); i += stripLen) {
stripLen = slice_.stripLength(i);
// this logic is never needed for slices at the edges
if (i == 0 || i + stripLen == slice_.length()) {
continue;
}
if (slice_.get(i) == CellState::EMPTY) {
if (slice_.get(i - 1) != CellState::CROSSED ||
slice_.get(i + stripLen) != CellState::CROSSED) {
continue;
}
// find holes that's smaller than all potential
// segments, and fill them with X.
auto seg = collidingSegments(i, i + stripLen - 1);
if (seg.first == seg.second) {
continue;
}
int minLen = len(seg.first);
for (int j = seg.first; j < seg.second; j++) {
if (minLen > len(j)) {
minLen = len(j);
}
}
if (minLen <= stripLen) {
continue;
}
slice_.setSegment(i, i + stripLen, CellState::CROSSED);
} else if (slice_.get(i) == CellState::SOLID) {
auto seg = collidingSegments(i, i + stripLen - 1);
if (seg.first == seg.second) {
continue;
}
if (seg.second - seg.first == 1 && done(seg.first)) {
continue;
}
int maxLen = len(seg.first);
int minLen = len(seg.first);
for (int j = seg.first; j < seg.second; j++) {
if (len(j) < minLen) {
minLen = len(j);
}
if (len(j) > maxLen) {
maxLen = len(j);
}
}
for (int j = i + stripLen; j < i + minLen && j < slice_.length(); j++) {
CellState s = slice_.get(j);
if (s == CellState::SOLID) {
break;
}
if (s == CellState::EMPTY) {
continue;
}
// we have strip "SSS X" and can prepend some S
if (slice_.setSegment(j - minLen, i, CellState::SOLID) > 0) {
stripLen += i - (j - minLen);
i = j - minLen;
}
break;
}
for (int j = i - 1; j >= i + stripLen - minLen && j >= 0; j--) {
CellState s = slice_.get(j);
if (s == CellState::SOLID) {
break;
}
if (s == CellState::EMPTY) {
continue;
}
// we have strip "X SSS" and can append some S
if (slice_.setSegment(i + stripLen, j + minLen + 1, CellState::SOLID) >
0) {
stripLen += j + minLen + 1 - (i + stripLen);
}
break;
}
if (maxLen == stripLen) {
slice_.setSegment(i - 1, i, CellState::CROSSED);
slice_.setSegment(i + stripLen, i + stripLen + 1, CellState::CROSSED);
}
}
}
return true;
}
bool Line::infer() {
// special case for no segments.
if (numSegments() == 0) {
slice_.setSegment(0, slice_.length(), CellState::CROSSED);
return true;
}
// update left and right bounts
if (!fitLeftMost(slice_, len_, lb_)) {
return false;
}
std::vector<int> len_reverse(len_);
std::reverse(len_reverse.begin(), len_reverse.end());
if (!fitLeftMost(slice_.reverse(), len_reverse, ub_)) {
return false;
}
updateStats();
if (!inferSegments()) {
return false;
}
if (!inferStrips()) {
return false;
}
return true;
}
Line::State::State(const Line &l) : lb(l.lb_), ub(l.ub_), done(l.done_) {}
Line::State Line::getState() const { return Line::State(*this); }
void Line::setState(Line::State &&s) {
lb_ = std::move(s.lb);
ub_ = std::move(s.ub);
done_ = std::move(s.done);
}
// Solver implementation
Solver::Solver(const Solver::Config &config,
std::vector<std::vector<int>> &&rows,
std::vector<std::vector<int>> &&cols)
: config_(config),
width_(cols.size()),
height_(rows.size()),
g_(cols.size() * rows.size(), CellState::EMPTY) {
for (int i = 0; i < height_; i++) {
lines_.push_back(
std::make_unique<Line>(*this, LineName::Row(i), std::move(rows[i])));
dirty_.push_back(LineName::Row(i));
}
for (int i = 0; i < width_; i++) {
lines_.push_back(
std::make_unique<Line>(*this, LineName::Column(i), std::move(cols[i])));
dirty_.push_back(LineName::Column(i));
}
}
void Solver::set(int x, int y, CellState val) {
if (val == get(x, y)) {
return;
}
if (get(x, y) != CellState::EMPTY) {
failed_ = true;
return;
}
g_[x + y * width_] = val;
if (lineName_.dir != Direction::ROW) {
markDirty(LineName::Row(y));
}
if (lineName_.dir != Direction::COLUMN) {
markDirty(LineName::Column(x));
}
};
void Solver::markDirty(LineName n) {
auto f = std::find(dirty_.begin(), dirty_.end(), n);
if (f == dirty_.end()) {
dirty_.push_back(n);
getLine(n).stats.numChanges++;
}
};
LineName Solver::getDirty() {
std::sort(dirty_.begin(), dirty_.end(),
[this](LineName a, LineName b) -> bool {
auto sa = config_.LineScore(this->getLine(a).stats);
auto sb = config_.LineScore(this->getLine(b).stats);
return sa < sb;
});
auto n = dirty_.back();
dirty_.pop_back();
return n;
};
void Solver::pushState() {
Solver::State s;
s.g = g_;
s.guessed = guessed_;
for (int i = 0; i < lines_.size(); i++) {
s.lines.push_back(lines_[i]->getState());
}
states_.push_back(s);
if (stats_.maxDepth < states_.size()) {
stats_.maxDepth = states_.size();
}
}
void Solver::popState() {
Solver::State &s = states_.back();
g_ = std::move(s.g);
guessed_ = s.guessed;
for (int i = 0; i < lines_.size(); i++) {
lines_[i]->setState(std::move(s.lines[i]));
}
dirty_.clear();
states_.pop_back();
}
// make inference on lines until all lines are checked.
bool Solver::infer() {
while (dirty_.size() > 0) {
lineName_ = getDirty();
Line &line = getLine(lineName_);
if (!line.infer()) {
return false;
};
stats_.lineCount++;
lineName_.dir = Direction::EMPTY;
if (failed_ || stats_.lineCount >= config_.maxLines) {
return false;
}
}
return true;
}
std::pair<double, CellState> Solver::Config::GuessScore(const Solver &s, int x,
int y) const {
double score = LineScore(s.getLine(LineName::Row(y)).stats) * rowCoef +
LineScore(s.getLine(LineName::Column(x)).stats) * colCoef;
int minX = std::min(x, s.width_ - 1 - x);
int minY = std::min(y, s.height_ - 1 - y);
if (minX < edgeScoreLen) {
score += edgeScore[minX];
}
if (minY < edgeScoreLen) {
score += edgeScore[minY];
}
std::vector<double> pattern = s.GridAt(x, y);
std::vector<double> pattern_score = n->evaluate(pattern);
CellState val = CellState::SOLID;
if (pattern_score[0] > pattern_score[1]) {
score += pattern_score[0];
val = CellState::CROSSED;
} else {
score += pattern_score[1];
}
// return std::make_pair(score, val);
return std::make_pair(score, val);
}
// picks an unwritten cell and make a guess. Returs object like
// {x,y,val}. Returns empty guess if everything has been filled.
Solver::Guess Solver::guess() {
auto r = Solver::Guess::Empty();
double maxScore = -std::numeric_limits<double>::infinity();
for (int x = 0; x < width_; x++) {
for (int y = 0; y < height_; y++) {
if (get(x, y) != CellState::EMPTY) {
continue;
}
CellState val;
double score;
std::tie(score, val) = config_.GuessScore(*this, x, y);
if (score > maxScore) {
r.x = x;
r.y = y;
r.val = val;
maxScore = score;
}
}
}
return r;
}
// Returns a vector representing the grid around point x,y.
std::vector<double> Solver::GridAt(int x, int y) const {
std::vector<double> g;
g.reserve(gridSize);
for (int i = x - gridHalfEdge; i <= x + gridHalfEdge; i++) {
for (int j = y - gridHalfEdge; j <= y + gridHalfEdge; j++) {
if (i < 0 || i >= width_ || j < 0 || j >= height_) {
g.push_back(-1);
continue;
}
switch (get(i, j)) {
case CellState::SOLID:
g.push_back(1);
break;
case CellState::EMPTY:
g.push_back(0);
break;
case CellState::CROSSED:
g.push_back(-1);
break;
}
}
}
return g;
};
bool Solver::solve() {
while (true) {
if (!infer() || failed_) {
if (states_.size() == 0) {
return false;
}
failed_ = false;
popState();
set(guessed_.x, guessed_.y,
guessed_.val == CellState::SOLID ? CellState::CROSSED
: CellState::SOLID);
stats_.wrongGuesses++;
guessed_ = Solver::Guess::Empty();
} else {
auto g = guess();
if (g.isEmpty()) {
return true;
}
guessed_ = g;
pushState();
set(g.x, g.y, g.val);
}
}
}
void Solver::printGrid() {
for (int y = 0; y < height_; y++) {
for (int x = 0; x < width_; x++) {
switch (get(x, y)) {
case CellState::EMPTY:
std::cout << ' ';
break;
case CellState::SOLID:
std::cout << '#';
break;
case CellState::CROSSED:
std::cout << '.';
break;
}
}
std::cout << '\n';
}
}