| 1 | #ifndef LATIN_H |
| 2 | #define LATIN_H |
| 3 | |
| 4 | #include "puzzles.h" |
| 5 | |
| 6 | typedef unsigned char digit; |
| 7 | |
| 8 | /* --- Solver structures, definitions --- */ |
| 9 | |
| 10 | #ifdef STANDALONE_SOLVER |
| 11 | int solver_show_working, solver_recurse_depth; |
| 12 | #endif |
| 13 | |
| 14 | struct latin_solver { |
| 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 */ |
| 19 | |
| 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 */ |
| 22 | }; |
| 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)]) |
| 25 | |
| 26 | #define gridpos(x,y) ((y)*solver->o+(x)) |
| 27 | #define grid(x,y) (solver->grid[gridpos(x,y)]) |
| 28 | |
| 29 | /* A solo solver using this code would need these defined. See solo.c. */ |
| 30 | #ifndef YTRANS |
| 31 | #define YTRANS(y) (y) |
| 32 | #endif |
| 33 | #ifndef YUNTRANS |
| 34 | #define YUNTRANS(y) (y) |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | /* --- Solver individual strategies --- */ |
| 39 | |
| 40 | /* Place a value at a specific location. */ |
| 41 | void latin_solver_place(struct latin_solver *solver, int x, int y, int n); |
| 42 | |
| 43 | /* Positional elimination. */ |
| 44 | int latin_solver_elim(struct latin_solver *solver, int start, int step |
| 45 | #ifdef STANDALONE_SOLVER |
| 46 | , char *fmt, ... |
| 47 | #endif |
| 48 | ); |
| 49 | |
| 50 | struct latin_solver_scratch; /* private to latin.c */ |
| 51 | /* Set elimination */ |
| 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 |
| 56 | , char *fmt, ... |
| 57 | #endif |
| 58 | ); |
| 59 | |
| 60 | /* Forcing chains */ |
| 61 | int latin_solver_forcing(struct latin_solver *solver, |
| 62 | struct latin_solver_scratch *scratch); |
| 63 | |
| 64 | |
| 65 | /* --- Solver allocation --- */ |
| 66 | |
| 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); |
| 73 | |
| 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); |
| 78 | |
| 79 | |
| 80 | /* --- Solver guts --- */ |
| 81 | |
| 82 | /* Looped positional elimination */ |
| 83 | int latin_solver_diff_simple(struct latin_solver *solver); |
| 84 | |
| 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, |
| 89 | int extreme); |
| 90 | |
| 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); |
| 97 | |
| 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); |
| 102 | |
| 103 | void latin_solver_debug(unsigned char *cube, int o); |
| 104 | |
| 105 | /* --- Generation and checking --- */ |
| 106 | |
| 107 | digit *latin_generate_quick(int o, random_state *rs); |
| 108 | digit *latin_generate(int o, random_state *rs); |
| 109 | |
| 110 | int latin_check(digit *sq, int order); /* !0 => not a latin square */ |
| 111 | |
| 112 | void latin_debug(digit *sq, int order); |
| 113 | |
| 114 | #endif |