6
typedef unsigned char digit;
8
/* --- Solver structures, definitions --- */
10
#ifdef STANDALONE_SOLVER
11
int solver_show_working, solver_recurse_depth;
15
int o; /* order of latin square */
16
unsigned char *cube; /* o^3, indexed by x, y, and digit:
17
TRUE in that position indicates a possibility */
18
digit *grid; /* o^2, indexed by x and y: for final deductions */
20
unsigned char *row; /* o^2: row[y*cr+n-1] TRUE if n is in row y */
21
unsigned char *col; /* o^2: col[x*cr+n-1] TRUE if n is in col x */
23
#define cubepos(x,y,n) (((x)*solver->o+(y))*solver->o+(n)-1)
24
#define cube(x,y,n) (solver->cube[cubepos(x,y,n)])
26
#define gridpos(x,y) ((y)*solver->o+(x))
27
#define grid(x,y) (solver->grid[gridpos(x,y)])
29
/* A solo solver using this code would need these defined. See solo.c. */
34
#define YUNTRANS(y) (y)
38
/* --- Solver individual strategies --- */
40
/* Place a value at a specific location. */
41
void latin_solver_place(struct latin_solver *solver, int x, int y, int n);
43
/* Positional elimination. */
44
int latin_solver_elim(struct latin_solver *solver, int start, int step
45
#ifdef STANDALONE_SOLVER
50
struct latin_solver_scratch; /* private to latin.c */
52
int latin_solver_set(struct latin_solver *solver,
53
struct latin_solver_scratch *scratch,
54
int start, int step1, int step2
55
#ifdef STANDALONE_SOLVER
61
int latin_solver_forcing(struct latin_solver *solver,
62
struct latin_solver_scratch *scratch);
65
/* --- Solver allocation --- */
67
/* Fills in (and allocates members for) a latin_solver struct.
68
* Will allocate members of snew, but not snew itself
69
* (allowing 'struct latin_solver' to be the first element in a larger
70
* struct, for example). */
71
void latin_solver_alloc(struct latin_solver *solver, digit *grid, int o);
72
void latin_solver_free(struct latin_solver *solver);
74
/* Allocates scratch space (for _set and _forcing) */
75
struct latin_solver_scratch *
76
latin_solver_new_scratch(struct latin_solver *solver);
77
void latin_solver_free_scratch(struct latin_solver_scratch *scratch);
80
/* --- Solver guts --- */
82
/* Looped positional elimination */
83
int latin_solver_diff_simple(struct latin_solver *solver);
85
/* Looped set elimination; *extreme is set if it used
86
* the more difficult single-number elimination. */
87
int latin_solver_diff_set(struct latin_solver *solver,
88
struct latin_solver_scratch *scratch,
91
typedef int (latin_solver_callback)(digit *, int, int, void*);
92
/* Use to provide a standard way of dealing with solvers which can recurse;
93
* pass in your enumeration for 'recursive diff' and your solver
94
* callback. Returns #solutions (0 == already solved). */
95
int latin_solver_recurse(struct latin_solver *solver, int recdiff,
96
latin_solver_callback cb, void *ctx);
98
/* Individual puzzles should use their enumerations for their
99
* own difficulty levels, ensuring they don't clash with these. */
100
enum { diff_impossible = 10, diff_ambiguous, diff_unfinished };
101
int latin_solver(digit *grid, int order, int maxdiff, void *unused);
103
void latin_solver_debug(unsigned char *cube, int o);
105
/* --- Generation and checking --- */
107
digit *latin_generate_quick(int o, random_state *rs);
108
digit *latin_generate(int o, random_state *rs);
110
int latin_check(digit *sq, int order); /* !0 => not a latin square */
112
void latin_debug(digit *sq, int order);