-
Notifications
You must be signed in to change notification settings - Fork 0
/
slant.c
2333 lines (2061 loc) · 63.5 KB
/
slant.c
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* slant.c: Puzzle from nikoli.co.jp involving drawing a diagonal
* line through each square of a grid.
*/
/*
* In this puzzle you have a grid of squares, each of which must
* contain a diagonal line; you also have clue numbers placed at
* _points_ of that grid, which means there's a (w+1) x (h+1) array
* of possible clue positions.
*
* I'm therefore going to adopt a rigid convention throughout this
* source file of using w and h for the dimensions of the grid of
* squares, and W and H for the dimensions of the grid of points.
* Thus, W == w+1 and H == h+1 always.
*
* Clue arrays will be W*H `signed char's, and the clue at each
* point will be a number from 0 to 4, or -1 if there's no clue.
*
* Solution arrays will be W*H `signed char's, and the number at
* each point will be +1 for a forward slash (/), -1 for a
* backslash (\), and 0 for unknown.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#ifdef NO_TGMATH_H
# include <math.h>
#else
# include <tgmath.h>
#endif
#include "puzzles.h"
enum {
COL_BACKGROUND,
COL_GRID,
COL_INK,
COL_SLANT1,
COL_SLANT2,
COL_ERROR,
COL_CURSOR,
COL_FILLEDSQUARE,
NCOLOURS
};
/*
* In standalone solver mode, `verbose' is a variable which can be
* set by command-line option; in debugging mode it's simply always
* true.
*/
#if defined STANDALONE_SOLVER
#define SOLVER_DIAGNOSTICS
static bool verbose = false;
#elif defined SOLVER_DIAGNOSTICS
#define verbose true
#endif
/*
* Difficulty levels. I do some macro ickery here to ensure that my
* enum and the various forms of my name list always match up.
*/
#define DIFFLIST(A) \
A(EASY,Easy,e) \
A(HARD,Hard,h)
#define ENUM(upper,title,lower) DIFF_ ## upper,
#define TITLE(upper,title,lower) #title,
#define ENCODE(upper,title,lower) #lower
#define CONFIG(upper,title,lower) ":" #title
enum { DIFFLIST(ENUM) DIFFCOUNT };
static char const *const slant_diffnames[] = { DIFFLIST(TITLE) };
static char const slant_diffchars[] = DIFFLIST(ENCODE);
#define DIFFCONFIG DIFFLIST(CONFIG)
struct game_params {
int w, h, diff;
};
typedef struct game_clues {
int w, h;
signed char *clues;
int *tmpdsf;
int refcount;
} game_clues;
#define ERR_VERTEX 1
#define ERR_SQUARE 2
struct game_state {
struct game_params p;
game_clues *clues;
signed char *soln;
unsigned char *errors;
bool completed;
bool used_solve; /* used to suppress completion flash */
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->w = ret->h = 8;
ret->diff = DIFF_EASY;
return ret;
}
static const struct game_params slant_presets[] = {
{5, 5, DIFF_EASY},
{5, 5, DIFF_HARD},
{8, 8, DIFF_EASY},
{8, 8, DIFF_HARD},
{12, 10, DIFF_EASY},
{12, 10, DIFF_HARD},
};
static bool game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char str[80];
if (i < 0 || i >= lenof(slant_presets))
return false;
ret = snew(game_params);
*ret = slant_presets[i];
sprintf(str, "%dx%d %s", ret->w, ret->h, slant_diffnames[ret->diff]);
*name = dupstr(str);
*params = ret;
return true;
}
static void free_params(game_params *params)
{
sfree(params);
}
static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static void decode_params(game_params *ret, char const *string)
{
ret->w = ret->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'x') {
string++;
ret->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
if (*string == 'd') {
int i;
string++;
for (i = 0; i < DIFFCOUNT; i++)
if (*string == slant_diffchars[i])
ret->diff = i;
if (*string) string++;
}
}
static char *encode_params(const game_params *params, bool full)
{
char data[256];
sprintf(data, "%dx%d", params->w, params->h);
if (full)
sprintf(data + strlen(data), "d%c", slant_diffchars[params->diff]);
return dupstr(data);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(4, config_item);
ret[0].name = "Width";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->w);
ret[0].u.string.sval = dupstr(buf);
ret[1].name = "Height";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->h);
ret[1].u.string.sval = dupstr(buf);
ret[2].name = "Difficulty";
ret[2].type = C_CHOICES;
ret[2].u.choices.choicenames = DIFFCONFIG;
ret[2].u.choices.selected = params->diff;
ret[3].name = NULL;
ret[3].type = C_END;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->w = atoi(cfg[0].u.string.sval);
ret->h = atoi(cfg[1].u.string.sval);
ret->diff = cfg[2].u.choices.selected;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
/*
* (At least at the time of writing this comment) The grid
* generator is actually capable of handling even zero grid
* dimensions without crashing. Puzzles with a zero-area grid
* are a bit boring, though, because they're already solved :-)
* And puzzles with a dimension of 1 can't be made Hard, which
* means the simplest thing is to forbid them altogether.
*/
if (params->w < 2 || params->h < 2)
return "Width and height must both be at least two";
if (params->w > INT_MAX / params->h)
return "Width times height must not be unreasonably large";
return NULL;
}
/*
* Scratch space for solver.
*/
struct solver_scratch {
/*
* Disjoint set forest which tracks the connected sets of
* points.
*/
DSF *connected;
/*
* Counts the number of possible exits from each connected set
* of points. (That is, the number of possible _simultaneous_
* exits: an unconnected point labelled 2 has an exit count of
* 2 even if all four possible edges are still under
* consideration.)
*/
int *exits;
/*
* Tracks whether each connected set of points includes a
* border point.
*/
bool *border;
/*
* Another disjoint set forest. This one tracks _squares_ which
* are known to slant in the same direction.
*/
DSF *equiv;
/*
* Stores slash values which we know for an equivalence class.
* When we fill in a square, we set slashval[canonify(x)] to
* the same value as soln[x], so that we can then spot other
* squares equivalent to it and fill them in immediately via
* their known equivalence.
*/
signed char *slashval;
/*
* Stores possible v-shapes. This array is w by h in size, but
* not every bit of every entry is meaningful. The bits mean:
*
* - bit 0 for a square means that that square and the one to
* its right might form a v-shape between them
* - bit 1 for a square means that that square and the one to
* its right might form a ^-shape between them
* - bit 2 for a square means that that square and the one
* below it might form a >-shape between them
* - bit 3 for a square means that that square and the one
* below it might form a <-shape between them
*
* Any starting 1 or 3 clue rules out four bits in this array
* immediately; a 2 clue propagates any ruled-out bit past it
* (if the two squares on one side of a 2 cannot be a v-shape,
* then neither can the two on the other side be the same
* v-shape); we can rule out further bits during play using
* partially filled 2 clues; whenever a pair of squares is
* known not to be _either_ kind of v-shape, we can mark them
* as equivalent.
*/
unsigned char *vbitmap;
/*
* Useful to have this information automatically passed to
* solver subroutines. (This pointer is not dynamically
* allocated by new_scratch and free_scratch.)
*/
const signed char *clues;
};
static struct solver_scratch *new_scratch(int w, int h)
{
int W = w+1, H = h+1;
struct solver_scratch *ret = snew(struct solver_scratch);
ret->connected = dsf_new(W*H);
ret->exits = snewn(W*H, int);
ret->border = snewn(W*H, bool);
ret->equiv = dsf_new(w*h);
ret->slashval = snewn(w*h, signed char);
ret->vbitmap = snewn(w*h, unsigned char);
return ret;
}
static void free_scratch(struct solver_scratch *sc)
{
sfree(sc->vbitmap);
sfree(sc->slashval);
dsf_free(sc->equiv);
sfree(sc->border);
sfree(sc->exits);
dsf_free(sc->connected);
sfree(sc);
}
/*
* Wrapper on dsf_merge() which updates the `exits' and `border'
* arrays.
*/
static void merge_vertices(DSF *connected,
struct solver_scratch *sc, int i, int j)
{
int exits = -1;
bool border = false; /* initialise to placate optimiser */
if (sc) {
i = dsf_canonify(connected, i);
j = dsf_canonify(connected, j);
/*
* We have used one possible exit from each of the two
* classes. Thus, the viable exit count of the new class is
* the sum of the old exit counts minus two.
*/
exits = sc->exits[i] + sc->exits[j] - 2;
border = sc->border[i] || sc->border[j];
}
dsf_merge(connected, i, j);
if (sc) {
i = dsf_canonify(connected, i);
sc->exits[i] = exits;
sc->border[i] = border;
}
}
/*
* Called when we have just blocked one way out of a particular
* point. If that point is a non-clue point (thus has a variable
* number of exits), we have therefore decreased its potential exit
* count, so we must decrement the exit count for the group as a
* whole.
*/
static void decr_exits(struct solver_scratch *sc, int i)
{
if (sc->clues[i] < 0) {
i = dsf_canonify(sc->connected, i);
sc->exits[i]--;
}
}
static void fill_square(int w, int h, int x, int y, int v,
signed char *soln,
DSF *connected, struct solver_scratch *sc)
{
int W = w+1 /*, H = h+1 */;
assert(x >= 0 && x < w && y >= 0 && y < h);
if (soln[y*w+x] != 0) {
return; /* do nothing */
}
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf(" placing %c in %d,%d\n", v == -1 ? '\\' : '/', x, y);
#endif
soln[y*w+x] = v;
if (sc) {
int c = dsf_canonify(sc->equiv, y*w+x);
sc->slashval[c] = v;
}
if (v < 0) {
merge_vertices(connected, sc, y*W+x, (y+1)*W+(x+1));
if (sc) {
decr_exits(sc, y*W+(x+1));
decr_exits(sc, (y+1)*W+x);
}
} else {
merge_vertices(connected, sc, y*W+(x+1), (y+1)*W+x);
if (sc) {
decr_exits(sc, y*W+x);
decr_exits(sc, (y+1)*W+(x+1));
}
}
}
static bool vbitmap_clear(int w, int h, struct solver_scratch *sc,
int x, int y, int vbits, const char *reason, ...)
{
bool done_something = false;
int vbit;
for (vbit = 1; vbit <= 8; vbit <<= 1)
if (vbits & sc->vbitmap[y*w+x] & vbit) {
done_something = true;
#ifdef SOLVER_DIAGNOSTICS
if (verbose) {
va_list ap;
printf("ruling out %c shape at (%d,%d)-(%d,%d) (",
"!v^!>!!!<"[vbit], x, y,
x+((vbit&0x3)!=0), y+((vbit&0xC)!=0));
va_start(ap, reason);
vprintf(reason, ap);
va_end(ap);
printf(")\n");
}
#endif
sc->vbitmap[y*w+x] &= ~vbit;
}
return done_something;
}
/*
* Solver. Returns 0 for impossibility, 1 for success, 2 for
* ambiguity or failure to converge.
*/
static int slant_solve(int w, int h, const signed char *clues,
signed char *soln, struct solver_scratch *sc,
int difficulty)
{
int W = w+1, H = h+1;
int x, y, i, j;
bool done_something;
/*
* Clear the output.
*/
memset(soln, 0, w*h);
sc->clues = clues;
/*
* Establish a disjoint set forest for tracking connectedness
* between grid points.
*/
dsf_reinit(sc->connected);
/*
* Establish a disjoint set forest for tracking which squares
* are known to slant in the same direction.
*/
dsf_reinit(sc->equiv);
/*
* Clear the slashval array.
*/
memset(sc->slashval, 0, w*h);
/*
* Set up the vbitmap array. Initially all types of v are possible.
*/
memset(sc->vbitmap, 0xF, w*h);
/*
* Initialise the `exits' and `border' arrays. These are used
* to do second-order loop avoidance: the dual of the no loops
* constraint is that every point must be somehow connected to
* the border of the grid (otherwise there would be a solid
* loop around it which prevented this).
*
* I define a `dead end' to be a connected group of points
* which contains no border point, and which can form at most
* one new connection outside itself. Then I forbid placing an
* edge so that it connects together two dead-end groups, since
* this would yield a non-border-connected isolated subgraph
* with no further scope to extend it.
*/
for (y = 0; y < H; y++)
for (x = 0; x < W; x++) {
if (y == 0 || y == H-1 || x == 0 || x == W-1)
sc->border[y*W+x] = true;
else
sc->border[y*W+x] = false;
if (clues[y*W+x] < 0)
sc->exits[y*W+x] = 4;
else
sc->exits[y*W+x] = clues[y*W+x];
}
/*
* Repeatedly try to deduce something until we can't.
*/
do {
done_something = false;
/*
* Any clue point with the number of remaining lines equal
* to zero or to the number of remaining undecided
* neighbouring squares can be filled in completely.
*/
for (y = 0; y < H; y++)
for (x = 0; x < W; x++) {
struct {
int pos, slash;
} neighbours[4];
int nneighbours;
int nu, nl, c, s, eq, eq2, last, meq, mj1, mj2;
if ((c = clues[y*W+x]) < 0)
continue;
/*
* We have a clue point. Start by listing its
* neighbouring squares, in order around the point,
* together with the type of slash that would be
* required in that square to connect to the point.
*/
nneighbours = 0;
if (x > 0 && y > 0) {
neighbours[nneighbours].pos = (y-1)*w+(x-1);
neighbours[nneighbours].slash = -1;
nneighbours++;
}
if (x > 0 && y < h) {
neighbours[nneighbours].pos = y*w+(x-1);
neighbours[nneighbours].slash = +1;
nneighbours++;
}
if (x < w && y < h) {
neighbours[nneighbours].pos = y*w+x;
neighbours[nneighbours].slash = -1;
nneighbours++;
}
if (x < w && y > 0) {
neighbours[nneighbours].pos = (y-1)*w+x;
neighbours[nneighbours].slash = +1;
nneighbours++;
}
/*
* Count up the number of undecided neighbours, and
* also the number of lines already present.
*
* If we're not on DIFF_EASY, then in this loop we
* also track whether we've seen two adjacent empty
* squares belonging to the same equivalence class
* (meaning they have the same type of slash). If
* so, we count them jointly as one line.
*/
nu = 0;
nl = c;
last = neighbours[nneighbours-1].pos;
if (soln[last] == 0)
eq = dsf_canonify(sc->equiv, last);
else
eq = -1;
meq = mj1 = mj2 = -1;
for (i = 0; i < nneighbours; i++) {
j = neighbours[i].pos;
s = neighbours[i].slash;
if (soln[j] == 0) {
nu++; /* undecided */
if (meq < 0 && difficulty > DIFF_EASY) {
eq2 = dsf_canonify(sc->equiv, j);
if (eq == eq2 && last != j) {
/*
* We've found an equivalent pair.
* Mark it. This also inhibits any
* further equivalence tracking
* around this square, since we can
* only handle one pair (and in
* particular we want to avoid
* being misled by two overlapping
* equivalence pairs).
*/
meq = eq;
mj1 = last;
mj2 = j;
nl--; /* count one line */
nu -= 2; /* and lose two undecideds */
} else
eq = eq2;
}
} else {
eq = -1;
if (soln[j] == s)
nl--; /* here's a line */
}
last = j;
}
/*
* Check the counts.
*/
if (nl < 0 || nl > nu) {
/*
* No consistent value for this at all!
*/
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf("need %d / %d lines around clue point at %d,%d!\n",
nl, nu, x, y);
#endif
return 0; /* impossible */
}
if (nu > 0 && (nl == 0 || nl == nu)) {
#ifdef SOLVER_DIAGNOSTICS
if (verbose) {
if (meq >= 0)
printf("partially (since %d,%d == %d,%d) ",
mj1%w, mj1/w, mj2%w, mj2/w);
printf("%s around clue point at %d,%d\n",
nl ? "filling" : "emptying", x, y);
}
#endif
for (i = 0; i < nneighbours; i++) {
j = neighbours[i].pos;
s = neighbours[i].slash;
if (soln[j] == 0 && j != mj1 && j != mj2)
fill_square(w, h, j%w, j/w, (nl ? s : -s), soln,
sc->connected, sc);
}
done_something = true;
} else if (nu == 2 && nl == 1 && difficulty > DIFF_EASY) {
/*
* If we have precisely two undecided squares
* and precisely one line to place between
* them, _and_ those squares are adjacent, then
* we can mark them as equivalent to one
* another.
*
* This even applies if meq >= 0: if we have a
* 2 clue point and two of its neighbours are
* already marked equivalent, we can indeed
* mark the other two as equivalent.
*
* We don't bother with this on DIFF_EASY,
* since we wouldn't have used the results
* anyway.
*/
last = -1;
for (i = 0; i < nneighbours; i++) {
j = neighbours[i].pos;
if (soln[j] == 0 && j != mj1 && j != mj2) {
if (last < 0)
last = i;
else if (last == i-1 || (last == 0 && i == 3))
break; /* found a pair */
}
}
if (i < nneighbours) {
int sv1, sv2;
assert(last >= 0);
/*
* neighbours[last] and neighbours[i] are
* the pair. Mark them equivalent.
*/
#ifdef SOLVER_DIAGNOSTICS
if (verbose) {
if (meq >= 0)
printf("since %d,%d == %d,%d, ",
mj1%w, mj1/w, mj2%w, mj2/w);
}
#endif
mj1 = neighbours[last].pos;
mj2 = neighbours[i].pos;
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf("clue point at %d,%d implies %d,%d == %d,"
"%d\n", x, y, mj1%w, mj1/w, mj2%w, mj2/w);
#endif
mj1 = dsf_canonify(sc->equiv, mj1);
sv1 = sc->slashval[mj1];
mj2 = dsf_canonify(sc->equiv, mj2);
sv2 = sc->slashval[mj2];
if (sv1 != 0 && sv2 != 0 && sv1 != sv2) {
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf("merged two equivalence classes with"
" different slash values!\n");
#endif
return 0;
}
sv1 = sv1 ? sv1 : sv2;
dsf_merge(sc->equiv, mj1, mj2);
mj1 = dsf_canonify(sc->equiv, mj1);
sc->slashval[mj1] = sv1;
}
}
}
if (done_something)
continue;
/*
* Failing that, we now apply the second condition, which
* is that no square may be filled in such a way as to form
* a loop. Also in this loop (since it's over squares
* rather than points), we check slashval to see if we've
* already filled in another square in the same equivalence
* class.
*
* The slashval check is disabled on DIFF_EASY, as is dead
* end avoidance. Only _immediate_ loop avoidance remains.
*/
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
bool fs, bs;
int v, c1, c2;
#ifdef SOLVER_DIAGNOSTICS
const char *reason = "<internal error>";
#endif
if (soln[y*w+x])
continue; /* got this one already */
fs = false;
bs = false;
if (difficulty > DIFF_EASY)
v = sc->slashval[dsf_canonify(sc->equiv, y*w+x)];
else
v = 0;
/*
* Try to rule out connectivity between (x,y) and
* (x+1,y+1); if successful, we will deduce that we
* must have a forward slash.
*/
c1 = dsf_canonify(sc->connected, y*W+x);
c2 = dsf_canonify(sc->connected, (y+1)*W+(x+1));
if (c1 == c2) {
fs = true;
#ifdef SOLVER_DIAGNOSTICS
reason = "simple loop avoidance";
#endif
}
if (difficulty > DIFF_EASY &&
!sc->border[c1] && !sc->border[c2] &&
sc->exits[c1] <= 1 && sc->exits[c2] <= 1) {
fs = true;
#ifdef SOLVER_DIAGNOSTICS
reason = "dead end avoidance";
#endif
}
if (v == +1) {
fs = true;
#ifdef SOLVER_DIAGNOSTICS
reason = "equivalence to an already filled square";
#endif
}
/*
* Now do the same between (x+1,y) and (x,y+1), to
* see if we are required to have a backslash.
*/
c1 = dsf_canonify(sc->connected, y*W+(x+1));
c2 = dsf_canonify(sc->connected, (y+1)*W+x);
if (c1 == c2) {
bs = true;
#ifdef SOLVER_DIAGNOSTICS
reason = "simple loop avoidance";
#endif
}
if (difficulty > DIFF_EASY &&
!sc->border[c1] && !sc->border[c2] &&
sc->exits[c1] <= 1 && sc->exits[c2] <= 1) {
bs = true;
#ifdef SOLVER_DIAGNOSTICS
reason = "dead end avoidance";
#endif
}
if (v == -1) {
bs = true;
#ifdef SOLVER_DIAGNOSTICS
reason = "equivalence to an already filled square";
#endif
}
if (fs && bs) {
/*
* No consistent value for this at all!
*/
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf("%d,%d has no consistent slash!\n", x, y);
#endif
return 0; /* impossible */
}
if (fs) {
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf("employing %s\n", reason);
#endif
fill_square(w, h, x, y, +1, soln, sc->connected, sc);
done_something = true;
} else if (bs) {
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf("employing %s\n", reason);
#endif
fill_square(w, h, x, y, -1, soln, sc->connected, sc);
done_something = true;
}
}
if (done_something)
continue;
/*
* Now see what we can do with the vbitmap array. All
* vbitmap deductions are disabled at Easy level.
*/
if (difficulty <= DIFF_EASY)
continue;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
int s, c;
/*
* Any line already placed in a square must rule
* out any type of v which contradicts it.
*/
if ((s = soln[y*w+x]) != 0) {
if (x > 0)
done_something |=
vbitmap_clear(w, h, sc, x-1, y, (s < 0 ? 0x1 : 0x2),
"contradicts known edge at (%d,%d)",x,y);
if (x+1 < w)
done_something |=
vbitmap_clear(w, h, sc, x, y, (s < 0 ? 0x2 : 0x1),
"contradicts known edge at (%d,%d)",x,y);
if (y > 0)
done_something |=
vbitmap_clear(w, h, sc, x, y-1, (s < 0 ? 0x4 : 0x8),
"contradicts known edge at (%d,%d)",x,y);
if (y+1 < h)
done_something |=
vbitmap_clear(w, h, sc, x, y, (s < 0 ? 0x8 : 0x4),
"contradicts known edge at (%d,%d)",x,y);
}
/*
* If both types of v are ruled out for a pair of
* adjacent squares, mark them as equivalent.
*/
if (x+1 < w && !(sc->vbitmap[y*w+x] & 0x3)) {
int n1 = y*w+x, n2 = y*w+(x+1);
if (dsf_canonify(sc->equiv, n1) !=
dsf_canonify(sc->equiv, n2)) {
dsf_merge(sc->equiv, n1, n2);
done_something = true;
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf("(%d,%d) and (%d,%d) must be equivalent"
" because both v-shapes are ruled out\n",
x, y, x+1, y);
#endif
}
}
if (y+1 < h && !(sc->vbitmap[y*w+x] & 0xC)) {
int n1 = y*w+x, n2 = (y+1)*w+x;
if (dsf_canonify(sc->equiv, n1) !=
dsf_canonify(sc->equiv, n2)) {
dsf_merge(sc->equiv, n1, n2);
done_something = true;
#ifdef SOLVER_DIAGNOSTICS
if (verbose)
printf("(%d,%d) and (%d,%d) must be equivalent"
" because both v-shapes are ruled out\n",
x, y, x, y+1);
#endif
}
}
/*
* The remaining work in this loop only works
* around non-edge clue points.
*/
if (y == 0 || x == 0)
continue;
if ((c = clues[y*W+x]) < 0)
continue;
/*
* x,y marks a clue point not on the grid edge. See
* if this clue point allows us to rule out any v
* shapes.
*/
if (c == 1) {
/*
* A 1 clue can never have any v shape pointing
* at it.
*/
done_something |=
vbitmap_clear(w, h, sc, x-1, y-1, 0x5,
"points at 1 clue at (%d,%d)", x, y);
done_something |=
vbitmap_clear(w, h, sc, x-1, y, 0x2,
"points at 1 clue at (%d,%d)", x, y);
done_something |=
vbitmap_clear(w, h, sc, x, y-1, 0x8,
"points at 1 clue at (%d,%d)", x, y);
} else if (c == 3) {
/*
* A 3 clue can never have any v shape pointing
* away from it.
*/
done_something |=
vbitmap_clear(w, h, sc, x-1, y-1, 0xA,
"points away from 3 clue at (%d,%d)", x, y);
done_something |=
vbitmap_clear(w, h, sc, x-1, y, 0x1,
"points away from 3 clue at (%d,%d)", x, y);
done_something |=
vbitmap_clear(w, h, sc, x, y-1, 0x4,
"points away from 3 clue at (%d,%d)", x, y);
} else if (c == 2) {
/*
* If a 2 clue has any kind of v ruled out on
* one side of it, the same v is ruled out on
* the other side.
*/
done_something |=
vbitmap_clear(w, h, sc, x-1, y-1,
(sc->vbitmap[(y )*w+(x-1)] & 0x3) ^ 0x3,
"propagated by 2 clue at (%d,%d)", x, y);
done_something |=
vbitmap_clear(w, h, sc, x-1, y-1,
(sc->vbitmap[(y-1)*w+(x )] & 0xC) ^ 0xC,
"propagated by 2 clue at (%d,%d)", x, y);
done_something |=
vbitmap_clear(w, h, sc, x-1, y,
(sc->vbitmap[(y-1)*w+(x-1)] & 0x3) ^ 0x3,
"propagated by 2 clue at (%d,%d)", x, y);
done_something |=
vbitmap_clear(w, h, sc, x, y-1,
(sc->vbitmap[(y-1)*w+(x-1)] & 0xC) ^ 0xC,
"propagated by 2 clue at (%d,%d)", x, y);
}
#undef CLEARBITS
}
} while (done_something);
/*
* Solver can make no more progress. See if the grid is full.
*/
for (i = 0; i < w*h; i++)
if (!soln[i])
return 2; /* failed to converge */
return 1; /* success */
}
/*
* Filled-grid generator.
*/
static void slant_generate(int w, int h, signed char *soln, random_state *rs)
{
int W = w+1, H = h+1;
int x, y, i;
DSF *connected;