-
Notifications
You must be signed in to change notification settings - Fork 1
/
latin.c
1436 lines (1273 loc) · 39.4 KB
/
latin.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
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include "puzzles.h"
#include "tree234.h"
#include "maxflow.h"
#ifdef STANDALONE_LATIN_TEST
#define STANDALONE_SOLVER
#endif
#include "latin.h"
/* --------------------------------------------------------
* Solver.
*/
static int latin_solver_top(struct latin_solver *solver, int maxdiff,
int diff_simple, int diff_set_0, int diff_set_1,
int diff_forcing, int diff_recursive,
usersolver_t const *usersolvers, void *ctx,
ctxnew_t ctxnew, ctxfree_t ctxfree);
#ifdef STANDALONE_SOLVER
int solver_show_working, solver_recurse_depth;
#endif
/*
* Function called when we are certain that a particular square has
* a particular number in it. The y-coordinate passed in here is
* transformed.
*/
void latin_solver_place(struct latin_solver *solver, int x, int y, int n)
{
int i, o = solver->o;
assert(n <= o);
assert(cube(x,y,n));
/*
* Rule out all other numbers in this square.
*/
for (i = 1; i <= o; i++)
if (i != n)
cube(x,y,i) = FALSE;
/*
* Rule out this number in all other positions in the row.
*/
for (i = 0; i < o; i++)
if (i != y)
cube(x,i,n) = FALSE;
/*
* Rule out this number in all other positions in the column.
*/
for (i = 0; i < o; i++)
if (i != x)
cube(i,y,n) = FALSE;
/*
* Enter the number in the result grid.
*/
solver->grid[y*o+x] = n;
/*
* Cross out this number from the list of numbers left to place
* in its row, its column and its block.
*/
solver->row[y*o+n-1] = solver->col[x*o+n-1] = TRUE;
}
int latin_solver_elim(struct latin_solver *solver, int start, int step
#ifdef STANDALONE_SOLVER
, char *fmt, ...
#endif
)
{
int o = solver->o;
#ifdef STANDALONE_SOLVER
char **names = solver->names;
#endif
int fpos, m, i;
/*
* Count the number of set bits within this section of the
* cube.
*/
m = 0;
fpos = -1;
for (i = 0; i < o; i++)
if (solver->cube[start+i*step]) {
fpos = start+i*step;
m++;
}
if (m == 1) {
int x, y, n;
assert(fpos >= 0);
n = 1 + fpos % o;
y = fpos / o;
x = y / o;
y %= o;
if (!solver->grid[y*o+x]) {
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
va_list ap;
printf("%*s", solver_recurse_depth*4, "");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf(":\n%*s placing %s at (%d,%d)\n",
solver_recurse_depth*4, "", names[n-1],
x+1, y+1);
}
#endif
latin_solver_place(solver, x, y, n);
return +1;
}
} else if (m == 0) {
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
va_list ap;
printf("%*s", solver_recurse_depth*4, "");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf(":\n%*s no possibilities available\n",
solver_recurse_depth*4, "");
}
#endif
return -1;
}
return 0;
}
struct latin_solver_scratch {
unsigned char *grid, *rowidx, *colidx, *set;
int *neighbours, *bfsqueue;
#ifdef STANDALONE_SOLVER
int *bfsprev;
#endif
};
int latin_solver_set(struct latin_solver *solver,
struct latin_solver_scratch *scratch,
int start, int step1, int step2
#ifdef STANDALONE_SOLVER
, char *fmt, ...
#endif
)
{
int o = solver->o;
#ifdef STANDALONE_SOLVER
char **names = solver->names;
#endif
int i, j, n, count;
unsigned char *grid = scratch->grid;
unsigned char *rowidx = scratch->rowidx;
unsigned char *colidx = scratch->colidx;
unsigned char *set = scratch->set;
/*
* We are passed a o-by-o matrix of booleans. Our first job
* is to winnow it by finding any definite placements - i.e.
* any row with a solitary 1 - and discarding that row and the
* column containing the 1.
*/
memset(rowidx, TRUE, o);
memset(colidx, TRUE, o);
for (i = 0; i < o; i++) {
int count = 0, first = -1;
for (j = 0; j < o; j++)
if (solver->cube[start+i*step1+j*step2])
first = j, count++;
if (count == 0) return -1;
if (count == 1)
rowidx[i] = colidx[first] = FALSE;
}
/*
* Convert each of rowidx/colidx from a list of 0s and 1s to a
* list of the indices of the 1s.
*/
for (i = j = 0; i < o; i++)
if (rowidx[i])
rowidx[j++] = i;
n = j;
for (i = j = 0; i < o; i++)
if (colidx[i])
colidx[j++] = i;
assert(n == j);
/*
* And create the smaller matrix.
*/
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
grid[i*o+j] = solver->cube[start+rowidx[i]*step1+colidx[j]*step2];
/*
* Having done that, we now have a matrix in which every row
* has at least two 1s in. Now we search to see if we can find
* a rectangle of zeroes (in the set-theoretic sense of
* `rectangle', i.e. a subset of rows crossed with a subset of
* columns) whose width and height add up to n.
*/
memset(set, 0, n);
count = 0;
while (1) {
/*
* We have a candidate set. If its size is <=1 or >=n-1
* then we move on immediately.
*/
if (count > 1 && count < n-1) {
/*
* The number of rows we need is n-count. See if we can
* find that many rows which each have a zero in all
* the positions listed in `set'.
*/
int rows = 0;
for (i = 0; i < n; i++) {
int ok = TRUE;
for (j = 0; j < n; j++)
if (set[j] && grid[i*o+j]) {
ok = FALSE;
break;
}
if (ok)
rows++;
}
/*
* We expect never to be able to get _more_ than
* n-count suitable rows: this would imply that (for
* example) there are four numbers which between them
* have at most three possible positions, and hence it
* indicates a faulty deduction before this point or
* even a bogus clue.
*/
if (rows > n - count) {
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
va_list ap;
printf("%*s", solver_recurse_depth*4,
"");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf(":\n%*s contradiction reached\n",
solver_recurse_depth*4, "");
}
#endif
return -1;
}
if (rows >= n - count) {
int progress = FALSE;
/*
* We've got one! Now, for each row which _doesn't_
* satisfy the criterion, eliminate all its set
* bits in the positions _not_ listed in `set'.
* Return +1 (meaning progress has been made) if we
* successfully eliminated anything at all.
*
* This involves referring back through
* rowidx/colidx in order to work out which actual
* positions in the cube to meddle with.
*/
for (i = 0; i < n; i++) {
int ok = TRUE;
for (j = 0; j < n; j++)
if (set[j] && grid[i*o+j]) {
ok = FALSE;
break;
}
if (!ok) {
for (j = 0; j < n; j++)
if (!set[j] && grid[i*o+j]) {
int fpos = (start+rowidx[i]*step1+
colidx[j]*step2);
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
int px, py, pn;
if (!progress) {
va_list ap;
printf("%*s", solver_recurse_depth*4,
"");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf(":\n");
}
pn = 1 + fpos % o;
py = fpos / o;
px = py / o;
py %= o;
printf("%*s ruling out %s at (%d,%d)\n",
solver_recurse_depth*4, "",
names[pn-1], px+1, py+1);
}
#endif
progress = TRUE;
solver->cube[fpos] = FALSE;
}
}
}
if (progress) {
return +1;
}
}
}
/*
* Binary increment: change the rightmost 0 to a 1, and
* change all 1s to the right of it to 0s.
*/
i = n;
while (i > 0 && set[i-1])
set[--i] = 0, count--;
if (i > 0)
set[--i] = 1, count++;
else
break; /* done */
}
return 0;
}
/*
* Look for forcing chains. A forcing chain is a path of
* pairwise-exclusive squares (i.e. each pair of adjacent squares
* in the path are in the same row, column or block) with the
* following properties:
*
* (a) Each square on the path has precisely two possible numbers.
*
* (b) Each pair of squares which are adjacent on the path share
* at least one possible number in common.
*
* (c) Each square in the middle of the path shares _both_ of its
* numbers with at least one of its neighbours (not the same
* one with both neighbours).
*
* These together imply that at least one of the possible number
* choices at one end of the path forces _all_ the rest of the
* numbers along the path. In order to make real use of this, we
* need further properties:
*
* (c) Ruling out some number N from the square at one end
* of the path forces the square at the other end to
* take number N.
*
* (d) The two end squares are both in line with some third
* square.
*
* (e) That third square currently has N as a possibility.
*
* If we can find all of that lot, we can deduce that at least one
* of the two ends of the forcing chain has number N, and that
* therefore the mutually adjacent third square does not.
*
* To find forcing chains, we're going to start a bfs at each
* suitable square, once for each of its two possible numbers.
*/
int latin_solver_forcing(struct latin_solver *solver,
struct latin_solver_scratch *scratch)
{
int o = solver->o;
#ifdef STANDALONE_SOLVER
char **names = solver->names;
#endif
int *bfsqueue = scratch->bfsqueue;
#ifdef STANDALONE_SOLVER
int *bfsprev = scratch->bfsprev;
#endif
unsigned char *number = scratch->grid;
int *neighbours = scratch->neighbours;
int x, y;
for (y = 0; y < o; y++)
for (x = 0; x < o; x++) {
int count, t, n;
/*
* If this square doesn't have exactly two candidate
* numbers, don't try it.
*
* In this loop we also sum the candidate numbers,
* which is a nasty hack to allow us to quickly find
* `the other one' (since we will shortly know there
* are exactly two).
*/
for (count = t = 0, n = 1; n <= o; n++)
if (cube(x, y, n))
count++, t += n;
if (count != 2)
continue;
/*
* Now attempt a bfs for each candidate.
*/
for (n = 1; n <= o; n++)
if (cube(x, y, n)) {
int orign, currn, head, tail;
/*
* Begin a bfs.
*/
orign = n;
memset(number, o+1, o*o);
head = tail = 0;
bfsqueue[tail++] = y*o+x;
#ifdef STANDALONE_SOLVER
bfsprev[y*o+x] = -1;
#endif
number[y*o+x] = t - n;
while (head < tail) {
int xx, yy, nneighbours, xt, yt, i;
xx = bfsqueue[head++];
yy = xx / o;
xx %= o;
currn = number[yy*o+xx];
/*
* Find neighbours of yy,xx.
*/
nneighbours = 0;
for (yt = 0; yt < o; yt++)
neighbours[nneighbours++] = yt*o+xx;
for (xt = 0; xt < o; xt++)
neighbours[nneighbours++] = yy*o+xt;
/*
* Try visiting each of those neighbours.
*/
for (i = 0; i < nneighbours; i++) {
int cc, tt, nn;
xt = neighbours[i] % o;
yt = neighbours[i] / o;
/*
* We need this square to not be
* already visited, and to include
* currn as a possible number.
*/
if (number[yt*o+xt] <= o)
continue;
if (!cube(xt, yt, currn))
continue;
/*
* Don't visit _this_ square a second
* time!
*/
if (xt == xx && yt == yy)
continue;
/*
* To continue with the bfs, we need
* this square to have exactly two
* possible numbers.
*/
for (cc = tt = 0, nn = 1; nn <= o; nn++)
if (cube(xt, yt, nn))
cc++, tt += nn;
if (cc == 2) {
bfsqueue[tail++] = yt*o+xt;
#ifdef STANDALONE_SOLVER
bfsprev[yt*o+xt] = yy*o+xx;
#endif
number[yt*o+xt] = tt - currn;
}
/*
* One other possibility is that this
* might be the square in which we can
* make a real deduction: if it's
* adjacent to x,y, and currn is equal
* to the original number we ruled out.
*/
if (currn == orign &&
(xt == x || yt == y)) {
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
char *sep = "";
int xl, yl;
printf("%*sforcing chain, %s at ends of ",
solver_recurse_depth*4, "",
names[orign-1]);
xl = xx;
yl = yy;
while (1) {
printf("%s(%d,%d)", sep, xl+1,
yl+1);
xl = bfsprev[yl*o+xl];
if (xl < 0)
break;
yl = xl / o;
xl %= o;
sep = "-";
}
printf("\n%*s ruling out %s at (%d,%d)\n",
solver_recurse_depth*4, "",
names[orign-1],
xt+1, yt+1);
}
#endif
cube(xt, yt, orign) = FALSE;
return 1;
}
}
}
}
}
return 0;
}
struct latin_solver_scratch *latin_solver_new_scratch(struct latin_solver *solver)
{
struct latin_solver_scratch *scratch = snew(struct latin_solver_scratch);
int o = solver->o;
scratch->grid = snewn(o*o, unsigned char);
scratch->rowidx = snewn(o, unsigned char);
scratch->colidx = snewn(o, unsigned char);
scratch->set = snewn(o, unsigned char);
scratch->neighbours = snewn(3*o, int);
scratch->bfsqueue = snewn(o*o, int);
#ifdef STANDALONE_SOLVER
scratch->bfsprev = snewn(o*o, int);
#endif
return scratch;
}
void latin_solver_free_scratch(struct latin_solver_scratch *scratch)
{
#ifdef STANDALONE_SOLVER
sfree(scratch->bfsprev);
#endif
sfree(scratch->bfsqueue);
sfree(scratch->neighbours);
sfree(scratch->set);
sfree(scratch->colidx);
sfree(scratch->rowidx);
sfree(scratch->grid);
sfree(scratch);
}
void latin_solver_alloc(struct latin_solver *solver, digit *grid, int o)
{
int x, y;
solver->o = o;
solver->cube = snewn(o*o*o, unsigned char);
solver->grid = grid; /* write straight back to the input */
memset(solver->cube, TRUE, o*o*o);
solver->row = snewn(o*o, unsigned char);
solver->col = snewn(o*o, unsigned char);
memset(solver->row, FALSE, o*o);
memset(solver->col, FALSE, o*o);
for (x = 0; x < o; x++)
for (y = 0; y < o; y++)
if (grid[y*o+x])
latin_solver_place(solver, x, y, grid[y*o+x]);
#ifdef STANDALONE_SOLVER
solver->names = NULL;
#endif
}
void latin_solver_free(struct latin_solver *solver)
{
sfree(solver->cube);
sfree(solver->row);
sfree(solver->col);
}
int latin_solver_diff_simple(struct latin_solver *solver)
{
int x, y, n, ret, o = solver->o;
#ifdef STANDALONE_SOLVER
char **names = solver->names;
#endif
/*
* Row-wise positional elimination.
*/
for (y = 0; y < o; y++)
for (n = 1; n <= o; n++)
if (!solver->row[y*o+n-1]) {
ret = latin_solver_elim(solver, cubepos(0,y,n), o*o
#ifdef STANDALONE_SOLVER
, "positional elimination,"
" %s in row %d", names[n-1],
y+1
#endif
);
if (ret != 0) return ret;
}
/*
* Column-wise positional elimination.
*/
for (x = 0; x < o; x++)
for (n = 1; n <= o; n++)
if (!solver->col[x*o+n-1]) {
ret = latin_solver_elim(solver, cubepos(x,0,n), o
#ifdef STANDALONE_SOLVER
, "positional elimination,"
" %s in column %d", names[n-1], x+1
#endif
);
if (ret != 0) return ret;
}
/*
* Numeric elimination.
*/
for (x = 0; x < o; x++)
for (y = 0; y < o; y++)
if (!solver->grid[y*o+x]) {
ret = latin_solver_elim(solver, cubepos(x,y,1), 1
#ifdef STANDALONE_SOLVER
, "numeric elimination at (%d,%d)",
x+1, y+1
#endif
);
if (ret != 0) return ret;
}
return 0;
}
int latin_solver_diff_set(struct latin_solver *solver,
struct latin_solver_scratch *scratch,
int extreme)
{
int x, y, n, ret, o = solver->o;
#ifdef STANDALONE_SOLVER
char **names = solver->names;
#endif
if (!extreme) {
/*
* Row-wise set elimination.
*/
for (y = 0; y < o; y++) {
ret = latin_solver_set(solver, scratch, cubepos(0,y,1), o*o, 1
#ifdef STANDALONE_SOLVER
, "set elimination, row %d", y+1
#endif
);
if (ret != 0) return ret;
}
/*
* Column-wise set elimination.
*/
for (x = 0; x < o; x++) {
ret = latin_solver_set(solver, scratch, cubepos(x,0,1), o, 1
#ifdef STANDALONE_SOLVER
, "set elimination, column %d", x+1
#endif
);
if (ret != 0) return ret;
}
} else {
/*
* Row-vs-column set elimination on a single number
* (much tricker for a human to do!)
*/
for (n = 1; n <= o; n++) {
ret = latin_solver_set(solver, scratch, cubepos(0,0,n), o*o, o
#ifdef STANDALONE_SOLVER
, "positional set elimination on %s",
names[n-1]
#endif
);
if (ret != 0) return ret;
}
}
return 0;
}
/*
* Returns:
* 0 for 'didn't do anything' implying it was already solved.
* -1 for 'impossible' (no solution)
* 1 for 'single solution'
* >1 for 'multiple solutions' (you don't get to know how many, and
* the first such solution found will be set.
*
* and this function may well assert if given an impossible board.
*/
static int latin_solver_recurse
(struct latin_solver *solver, int diff_simple, int diff_set_0,
int diff_set_1, int diff_forcing, int diff_recursive,
usersolver_t const *usersolvers, void *ctx,
ctxnew_t ctxnew, ctxfree_t ctxfree)
{
int best, bestcount;
int o = solver->o, x, y, n;
#ifdef STANDALONE_SOLVER
char **names = solver->names;
#endif
best = -1;
bestcount = o+1;
for (y = 0; y < o; y++)
for (x = 0; x < o; x++)
if (!solver->grid[y*o+x]) {
int count;
/*
* An unfilled square. Count the number of
* possible digits in it.
*/
count = 0;
for (n = 1; n <= o; n++)
if (cube(x,y,n))
count++;
/*
* We should have found any impossibilities
* already, so this can safely be an assert.
*/
assert(count > 1);
if (count < bestcount) {
bestcount = count;
best = y*o+x;
}
}
if (best == -1)
/* we were complete already. */
return 0;
else {
int i, j;
digit *list, *ingrid, *outgrid;
int diff = diff_impossible; /* no solution found yet */
/*
* Attempt recursion.
*/
y = best / o;
x = best % o;
list = snewn(o, digit);
ingrid = snewn(o*o, digit);
outgrid = snewn(o*o, digit);
memcpy(ingrid, solver->grid, o*o);
/* Make a list of the possible digits. */
for (j = 0, n = 1; n <= o; n++)
if (cube(x,y,n))
list[j++] = n;
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
char *sep = "";
printf("%*srecursing on (%d,%d) [",
solver_recurse_depth*4, "", x+1, y+1);
for (i = 0; i < j; i++) {
printf("%s%s", sep, names[list[i]-1]);
sep = " or ";
}
printf("]\n");
}
#endif
/*
* And step along the list, recursing back into the
* main solver at every stage.
*/
for (i = 0; i < j; i++) {
int ret;
void *newctx;
struct latin_solver subsolver;
memcpy(outgrid, ingrid, o*o);
outgrid[y*o+x] = list[i];
#ifdef STANDALONE_SOLVER
if (solver_show_working)
printf("%*sguessing %s at (%d,%d)\n",
solver_recurse_depth*4, "", names[list[i]-1], x+1, y+1);
solver_recurse_depth++;
#endif
if (ctxnew) {
newctx = ctxnew(ctx);
} else {
newctx = ctx;
}
latin_solver_alloc(&subsolver, outgrid, o);
#ifdef STANDALONE_SOLVER
subsolver.names = solver->names;
#endif
ret = latin_solver_top(&subsolver, diff_recursive,
diff_simple, diff_set_0, diff_set_1,
diff_forcing, diff_recursive,
usersolvers, newctx, ctxnew, ctxfree);
latin_solver_free(&subsolver);
if (ctxnew)
ctxfree(newctx);
#ifdef STANDALONE_SOLVER
solver_recurse_depth--;
if (solver_show_working) {
printf("%*sretracting %s at (%d,%d)\n",
solver_recurse_depth*4, "", names[list[i]-1], x+1, y+1);
}
#endif
/* we recurse as deep as we can, so we should never find
* find ourselves giving up on a puzzle without declaring it
* impossible. */
assert(ret != diff_unfinished);
/*
* If we have our first solution, copy it into the
* grid we will return.
*/
if (diff == diff_impossible && ret != diff_impossible)
memcpy(solver->grid, outgrid, o*o);
if (ret == diff_ambiguous)
diff = diff_ambiguous;
else if (ret == diff_impossible)
/* do not change our return value */;
else {
/* the recursion turned up exactly one solution */
if (diff == diff_impossible)
diff = diff_recursive;
else
diff = diff_ambiguous;
}
/*
* As soon as we've found more than one solution,
* give up immediately.
*/
if (diff == diff_ambiguous)
break;
}
sfree(outgrid);
sfree(ingrid);
sfree(list);
if (diff == diff_impossible)
return -1;
else if (diff == diff_ambiguous)
return 2;
else {
assert(diff == diff_recursive);
return 1;
}
}
}
static int latin_solver_top(struct latin_solver *solver, int maxdiff,
int diff_simple, int diff_set_0, int diff_set_1,
int diff_forcing, int diff_recursive,
usersolver_t const *usersolvers, void *ctx,
ctxnew_t ctxnew, ctxfree_t ctxfree)
{
struct latin_solver_scratch *scratch = latin_solver_new_scratch(solver);
int ret, diff = diff_simple;
assert(maxdiff <= diff_recursive);
/*
* Now loop over the grid repeatedly trying all permitted modes
* of reasoning. The loop terminates if we complete an
* iteration without making any progress; we then return
* failure or success depending on whether the grid is full or
* not.
*/
while (1) {
int i;
cont:
latin_solver_debug(solver->cube, solver->o);
for (i = 0; i <= maxdiff; i++) {
if (usersolvers[i])
ret = usersolvers[i](solver, ctx);
else
ret = 0;
if (ret == 0 && i == diff_simple)
ret = latin_solver_diff_simple(solver);
if (ret == 0 && i == diff_set_0)
ret = latin_solver_diff_set(solver, scratch, 0);
if (ret == 0 && i == diff_set_1)
ret = latin_solver_diff_set(solver, scratch, 1);
if (ret == 0 && i == diff_forcing)
ret = latin_solver_forcing(solver, scratch);
if (ret < 0) {
diff = diff_impossible;
goto got_result;
} else if (ret > 0) {
diff = max(diff, i);
goto cont;
}
}
/*
* If we reach here, we have made no deductions in this
* iteration, so the algorithm terminates.
*/
break;
}
/*
* Last chance: if we haven't fully solved the puzzle yet, try
* recursing based on guesses for a particular square. We pick
* one of the most constrained empty squares we can find, which
* has the effect of pruning the search tree as much as
* possible.
*/
if (maxdiff == diff_recursive) {
int nsol = latin_solver_recurse(solver,
diff_simple, diff_set_0, diff_set_1,
diff_forcing, diff_recursive,
usersolvers, ctx, ctxnew, ctxfree);
if (nsol < 0) diff = diff_impossible;
else if (nsol == 1) diff = diff_recursive;
else if (nsol > 1) diff = diff_ambiguous;
/* if nsol == 0 then we were complete anyway
* (and thus don't need to change diff) */
} else {
/*
* We're forbidden to use recursion, so we just see whether
* our grid is fully solved, and return diff_unfinished
* otherwise.
*/
int x, y, o = solver->o;
for (y = 0; y < o; y++)
for (x = 0; x < o; x++)
if (!solver->grid[y*o+x])
diff = diff_unfinished;
}
got_result:
#ifdef STANDALONE_SOLVER
if (solver_show_working)
printf("%*s%s found\n",
solver_recurse_depth*4, "",
diff == diff_impossible ? "no solution (impossible)" :
diff == diff_unfinished ? "no solution (unfinished)" :
diff == diff_ambiguous ? "multiple solutions" :
"one solution");
#endif
latin_solver_free_scratch(scratch);
return diff;
}
int latin_solver_main(struct latin_solver *solver, int maxdiff,
int diff_simple, int diff_set_0, int diff_set_1,
int diff_forcing, int diff_recursive,
usersolver_t const *usersolvers, void *ctx,
ctxnew_t ctxnew, ctxfree_t ctxfree)
{
int diff;
#ifdef STANDALONE_SOLVER
int o = solver->o;
char *text = NULL, **names = NULL;
#endif
#ifdef STANDALONE_SOLVER
if (!solver->names) {
char *p;
int i;
text = snewn(40 * o, char);
p = text;