+ for (n = 1; n <= cr; n++) {
+ if (!cube(x1, y1, n))
+ continue;
+ for (j = 0; j < cr; j++)
+ numbersleft[j] = cube(x2, y2, j+1);
+
+ /*
+ * Go over every possible subset of each neighbour list,
+ * and see if its union of possible numbers minus n has the
+ * same size as the subset. If so, add the numbers in that
+ * subset to the set of things which would be ruled out
+ * from (x2,y2) if n were placed at (x1,y1).
+ */
+ memset(set, 0, nnb);
+ count = 0;
+ while (1) {
+ /*
+ * Binary increment: change the rightmost 0 to a 1, and
+ * change all 1s to the right of it to 0s.
+ */
+ i = nnb;
+ while (i > 0 && set[i-1])
+ set[--i] = 0, count--;
+ if (i > 0)
+ set[--i] = 1, count++;
+ else
+ break; /* done */
+
+ /*
+ * Examine this subset of each neighbour set.
+ */
+ for (nbi = 0; nbi < 2; nbi++) {
+ int *nbs = nb[nbi];
+
+ memset(numbers, 0, cr);
+
+ for (i = 0; i < nnb; i++)
+ if (set[i])
+ for (j = 0; j < cr; j++)
+ if (j != n-1 && usage->cube[nbs[i] + j])
+ numbers[j] = 1;
+
+ for (i = j = 0; j < cr; j++)
+ i += numbers[j];
+
+ if (i == count) {
+ /*
+ * Got one. This subset of nbs, in the absence
+ * of n, would definitely contain all the
+ * numbers listed in `numbers'. Rule them out
+ * of `numbersleft'.
+ */
+ for (j = 0; j < cr; j++)
+ if (numbers[j])
+ numbersleft[j] = 0;
+ }
+ }
+ }
+
+ /*
+ * If we've got nothing left in `numbersleft', we have a
+ * successful mutual neighbour elimination.
+ */
+ for (j = 0; j < cr; j++)
+ if (numbersleft[j])
+ break;
+
+ if (j == cr) {
+#ifdef STANDALONE_SOLVER
+ if (solver_show_working) {
+ printf("%*smutual neighbour elimination, (%d,%d) vs (%d,%d):\n",
+ solver_recurse_depth*4, "",
+ 1+x1, 1+YUNTRANS(y1), 1+x2, 1+YUNTRANS(y2));
+ printf("%*s ruling out %d at (%d,%d)\n",
+ solver_recurse_depth*4, "",
+ n, 1+x1, 1+YUNTRANS(y1));
+ }
+#endif
+ cube(x1, y1, n) = FALSE;
+ return +1;
+ }
+ }
+
+ return 0; /* nothing found */
+}
+
+/*
+ * 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.
+ */
+static int solver_forcing(struct solver_usage *usage,
+ struct solver_scratch *scratch)
+{
+ int c = usage->c, r = usage->r, cr = c*r;
+ 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 < cr; y++)
+ for (x = 0; x < cr; 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 <= cr; n++)
+ if (cube(x, y, n))
+ count++, t += n;
+ if (count != 2)
+ continue;
+
+ /*
+ * Now attempt a bfs for each candidate.
+ */
+ for (n = 1; n <= cr; n++)
+ if (cube(x, y, n)) {
+ int orign, currn, head, tail;
+
+ /*
+ * Begin a bfs.
+ */
+ orign = n;
+
+ memset(number, cr+1, cr*cr);
+ head = tail = 0;
+ bfsqueue[tail++] = y*cr+x;
+#ifdef STANDALONE_SOLVER
+ bfsprev[y*cr+x] = -1;
+#endif
+ number[y*cr+x] = t - n;
+
+ while (head < tail) {
+ int xx, yy, nneighbours, xt, yt, xblk, i;
+
+ xx = bfsqueue[head++];
+ yy = xx / cr;
+ xx %= cr;
+
+ currn = number[yy*cr+xx];
+
+ /*
+ * Find neighbours of yy,xx.
+ */
+ nneighbours = 0;
+ for (yt = 0; yt < cr; yt++)
+ neighbours[nneighbours++] = yt*cr+xx;
+ for (xt = 0; xt < cr; xt++)
+ neighbours[nneighbours++] = yy*cr+xt;
+ xblk = xx - (xx % r);
+ for (yt = yy % r; yt < cr; yt += r)
+ for (xt = xblk; xt < xblk+r; xt++)
+ neighbours[nneighbours++] = yt*cr+xt;
+
+ /*
+ * Try visiting each of those neighbours.
+ */
+ for (i = 0; i < nneighbours; i++) {
+ int cc, tt, nn;
+
+ xt = neighbours[i] % cr;
+ yt = neighbours[i] / cr;
+
+ /*
+ * We need this square to not be
+ * already visited, and to include
+ * currn as a possible number.
+ */
+ if (number[yt*cr+xt] <= cr)
+ 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 <= cr; nn++)
+ if (cube(xt, yt, nn))
+ cc++, tt += nn;
+ if (cc == 2) {
+ bfsqueue[tail++] = yt*cr+xt;
+#ifdef STANDALONE_SOLVER
+ bfsprev[yt*cr+xt] = yy*cr+xx;
+#endif
+ number[yt*cr+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 ||
+ (xt / r == x / r && yt % r == y % r))) {
+#ifdef STANDALONE_SOLVER
+ if (solver_show_working) {
+ char *sep = "";
+ int xl, yl;
+ printf("%*sforcing chain, %d at ends of ",
+ solver_recurse_depth*4, "", orign);
+ xl = xx;
+ yl = yy;
+ while (1) {
+ printf("%s(%d,%d)", sep, 1+xl,
+ 1+YUNTRANS(yl));
+ xl = bfsprev[yl*cr+xl];
+ if (xl < 0)
+ break;
+ yl = xl / cr;
+ xl %= cr;
+ sep = "-";
+ }
+ printf("\n%*s ruling out %d at (%d,%d)\n",
+ solver_recurse_depth*4, "",
+ orign, 1+xt, 1+YUNTRANS(yt));
+ }
+#endif
+ cube(xt, yt, orign) = FALSE;
+ return 1;
+ }
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+static struct solver_scratch *solver_new_scratch(struct solver_usage *usage)
+{
+ struct solver_scratch *scratch = snew(struct solver_scratch);
+ int cr = usage->cr;
+ scratch->grid = snewn(cr*cr, unsigned char);
+ scratch->rowidx = snewn(cr, unsigned char);
+ scratch->colidx = snewn(cr, unsigned char);
+ scratch->set = snewn(cr, unsigned char);
+ scratch->neighbours = snewn(3*cr, int);
+ scratch->bfsqueue = snewn(cr*cr, int);
+#ifdef STANDALONE_SOLVER
+ scratch->bfsprev = snewn(cr*cr, int);
+#endif
+ return scratch;
+}
+
+static void solver_free_scratch(struct 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);
+}
+
+static int solver(int c, int r, digit *grid, int maxdiff)
+{
+ struct solver_usage *usage;
+ struct solver_scratch *scratch;
+ int cr = c*r;
+ int x, y, x2, y2, n, ret;
+ int diff = DIFF_BLOCK;
+
+ /*
+ * Set up a usage structure as a clean slate (everything
+ * possible).
+ */
+ usage = snew(struct solver_usage);
+ usage->c = c;
+ usage->r = r;
+ usage->cr = cr;
+ usage->cube = snewn(cr*cr*cr, unsigned char);
+ usage->grid = grid; /* write straight back to the input */
+ memset(usage->cube, TRUE, cr*cr*cr);
+
+ usage->row = snewn(cr * cr, unsigned char);
+ usage->col = snewn(cr * cr, unsigned char);
+ usage->blk = snewn(cr * cr, unsigned char);
+ memset(usage->row, FALSE, cr * cr);
+ memset(usage->col, FALSE, cr * cr);
+ memset(usage->blk, FALSE, cr * cr);
+
+ scratch = solver_new_scratch(usage);
+
+ /*
+ * Place all the clue numbers we are given.
+ */
+ for (x = 0; x < cr; x++)
+ for (y = 0; y < cr; y++)
+ if (grid[y*cr+x])
+ solver_place(usage, x, YTRANS(y), grid[y*cr+x]);
+
+ /*
+ * 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) {
+ /*
+ * I'd like to write `continue;' inside each of the
+ * following loops, so that the solver returns here after
+ * making some progress. However, I can't specify that I
+ * want to continue an outer loop rather than the innermost
+ * one, so I'm apologetically resorting to a goto.
+ */
+ cont:
+
+ /*
+ * Blockwise positional elimination.
+ */
+ for (x = 0; x < cr; x += r)
+ for (y = 0; y < r; y++)
+ for (n = 1; n <= cr; n++)
+ if (!usage->blk[(y*c+(x/r))*cr+n-1]) {
+ ret = solver_elim(usage, cubepos(x,y,n), r*cr
+#ifdef STANDALONE_SOLVER
+ , "positional elimination,"
+ " %d in block (%d,%d)", n, 1+x/r, 1+y
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_BLOCK);
+ goto cont;
+ }
+ }
+
+ if (maxdiff <= DIFF_BLOCK)
+ break;
+
+ /*
+ * Row-wise positional elimination.
+ */
+ for (y = 0; y < cr; y++)
+ for (n = 1; n <= cr; n++)
+ if (!usage->row[y*cr+n-1]) {
+ ret = solver_elim(usage, cubepos(0,y,n), cr*cr
+#ifdef STANDALONE_SOLVER
+ , "positional elimination,"
+ " %d in row %d", n, 1+YUNTRANS(y)
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_SIMPLE);
+ goto cont;
+ }
+ }
+ /*
+ * Column-wise positional elimination.
+ */
+ for (x = 0; x < cr; x++)
+ for (n = 1; n <= cr; n++)
+ if (!usage->col[x*cr+n-1]) {
+ ret = solver_elim(usage, cubepos(x,0,n), cr
+#ifdef STANDALONE_SOLVER
+ , "positional elimination,"
+ " %d in column %d", n, 1+x
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_SIMPLE);
+ goto cont;
+ }
+ }
+
+ /*
+ * Numeric elimination.
+ */
+ for (x = 0; x < cr; x++)
+ for (y = 0; y < cr; y++)
+ if (!usage->grid[YUNTRANS(y)*cr+x]) {
+ ret = solver_elim(usage, cubepos(x,y,1), 1
+#ifdef STANDALONE_SOLVER
+ , "numeric elimination at (%d,%d)", 1+x,
+ 1+YUNTRANS(y)
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_SIMPLE);
+ goto cont;
+ }
+ }
+
+ if (maxdiff <= DIFF_SIMPLE)
+ break;
+
+ /*
+ * Intersectional analysis, rows vs blocks.
+ */
+ for (y = 0; y < cr; y++)
+ for (x = 0; x < cr; x += r)
+ for (n = 1; n <= cr; n++)
+ /*
+ * solver_intersect() never returns -1.
+ */
+ if (!usage->row[y*cr+n-1] &&
+ !usage->blk[((y%r)*c+(x/r))*cr+n-1] &&
+ (solver_intersect(usage, cubepos(0,y,n), cr*cr,
+ cubepos(x,y%r,n), r*cr
+#ifdef STANDALONE_SOLVER
+ , "intersectional analysis,"
+ " %d in row %d vs block (%d,%d)",
+ n, 1+YUNTRANS(y), 1+x/r, 1+y%r
+#endif
+ ) ||
+ solver_intersect(usage, cubepos(x,y%r,n), r*cr,
+ cubepos(0,y,n), cr*cr
+#ifdef STANDALONE_SOLVER
+ , "intersectional analysis,"
+ " %d in block (%d,%d) vs row %d",
+ n, 1+x/r, 1+y%r, 1+YUNTRANS(y)
+#endif
+ ))) {
+ diff = max(diff, DIFF_INTERSECT);
+ goto cont;
+ }
+
+ /*
+ * Intersectional analysis, columns vs blocks.
+ */
+ for (x = 0; x < cr; x++)
+ for (y = 0; y < r; y++)
+ for (n = 1; n <= cr; n++)
+ if (!usage->col[x*cr+n-1] &&
+ !usage->blk[(y*c+(x/r))*cr+n-1] &&
+ (solver_intersect(usage, cubepos(x,0,n), cr,
+ cubepos((x/r)*r,y,n), r*cr
+#ifdef STANDALONE_SOLVER
+ , "intersectional analysis,"
+ " %d in column %d vs block (%d,%d)",
+ n, 1+x, 1+x/r, 1+y
+#endif
+ ) ||
+ solver_intersect(usage, cubepos((x/r)*r,y,n), r*cr,
+ cubepos(x,0,n), cr
+#ifdef STANDALONE_SOLVER
+ , "intersectional analysis,"
+ " %d in block (%d,%d) vs column %d",
+ n, 1+x/r, 1+y, 1+x
+#endif
+ ))) {
+ diff = max(diff, DIFF_INTERSECT);
+ goto cont;
+ }
+
+ if (maxdiff <= DIFF_INTERSECT)
+ break;
+
+ /*
+ * Blockwise set elimination.
+ */
+ for (x = 0; x < cr; x += r)
+ for (y = 0; y < r; y++) {
+ ret = solver_set(usage, scratch, cubepos(x,y,1), r*cr, 1
+#ifdef STANDALONE_SOLVER
+ , "set elimination, block (%d,%d)", 1+x/r, 1+y
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_SET);
+ goto cont;
+ }
+ }
+
+ /*
+ * Row-wise set elimination.
+ */
+ for (y = 0; y < cr; y++) {
+ ret = solver_set(usage, scratch, cubepos(0,y,1), cr*cr, 1
+#ifdef STANDALONE_SOLVER
+ , "set elimination, row %d", 1+YUNTRANS(y)
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_SET);
+ goto cont;
+ }
+ }
+
+ /*
+ * Column-wise set elimination.
+ */
+ for (x = 0; x < cr; x++) {
+ ret = solver_set(usage, scratch, cubepos(x,0,1), cr, 1
+#ifdef STANDALONE_SOLVER
+ , "set elimination, column %d", 1+x
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_SET);
+ goto cont;
+ }
+ }
+
+ /*
+ * Row-vs-column set elimination on a single number.
+ */
+ for (n = 1; n <= cr; n++) {
+ ret = solver_set(usage, scratch, cubepos(0,0,n), cr*cr, cr
+#ifdef STANDALONE_SOLVER
+ , "positional set elimination, number %d", n
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_EXTREME);
+ goto cont;
+ }
+ }
+
+ /*
+ * Mutual neighbour elimination.
+ */
+ for (y = 0; y+1 < cr; y++) {
+ for (x = 0; x+1 < cr; x++) {
+ for (y2 = y+1; y2 < cr; y2++) {
+ for (x2 = x+1; x2 < cr; x2++) {
+ /*
+ * Can't do mutual neighbour elimination
+ * between elements of the same actual
+ * block.
+ */
+ if (x/r == x2/r && y%r == y2%r)
+ continue;
+
+ /*
+ * Otherwise, try (x,y) vs (x2,y2) in both
+ * directions, and likewise (x2,y) vs
+ * (x,y2).
+ */
+ if (!usage->grid[YUNTRANS(y)*cr+x] &&
+ !usage->grid[YUNTRANS(y2)*cr+x2] &&
+ (solver_mne(usage, scratch, x, y, x2, y2) ||
+ solver_mne(usage, scratch, x2, y2, x, y))) {
+ diff = max(diff, DIFF_EXTREME);
+ goto cont;
+ }
+ if (!usage->grid[YUNTRANS(y)*cr+x2] &&
+ !usage->grid[YUNTRANS(y2)*cr+x] &&
+ (solver_mne(usage, scratch, x2, y, x, y2) ||
+ solver_mne(usage, scratch, x, y2, x2, y))) {
+ diff = max(diff, DIFF_EXTREME);
+ goto cont;
+ }
+ }
+ }
+ }
+ }
+
+ /*
+ * Forcing chains.
+ */
+ if (solver_forcing(usage, scratch)) {
+ diff = max(diff, DIFF_EXTREME);
+ 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 best, bestcount;
+
+ best = -1;
+ bestcount = cr+1;
+
+ for (y = 0; y < cr; y++)
+ for (x = 0; x < cr; x++)
+ if (!grid[y*cr+x]) {
+ int count;
+
+ /*
+ * An unfilled square. Count the number of
+ * possible digits in it.
+ */
+ count = 0;
+ for (n = 1; n <= cr; n++)
+ if (cube(x,YTRANS(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*cr+x;
+ }
+ }
+
+ if (best != -1) {
+ int i, j;
+ digit *list, *ingrid, *outgrid;
+
+ diff = DIFF_IMPOSSIBLE; /* no solution found yet */
+
+ /*
+ * Attempt recursion.
+ */
+ y = best / cr;
+ x = best % cr;
+
+ list = snewn(cr, digit);
+ ingrid = snewn(cr * cr, digit);
+ outgrid = snewn(cr * cr, digit);
+ memcpy(ingrid, grid, cr * cr);
+
+ /* Make a list of the possible digits. */
+ for (j = 0, n = 1; n <= cr; n++)
+ if (cube(x,YTRANS(y),n))
+ list[j++] = n;
+
+#ifdef STANDALONE_SOLVER
+ if (solver_show_working) {
+ char *sep = "";
+ printf("%*srecursing on (%d,%d) [",
+ solver_recurse_depth*4, "", x, y);
+ for (i = 0; i < j; i++) {
+ printf("%s%d", sep, list[i]);
+ 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;
+
+ memcpy(outgrid, ingrid, cr * cr);
+ outgrid[y*cr+x] = list[i];
+
+#ifdef STANDALONE_SOLVER
+ if (solver_show_working)
+ printf("%*sguessing %d at (%d,%d)\n",
+ solver_recurse_depth*4, "", list[i], x, y);
+ solver_recurse_depth++;
+#endif
+
+ ret = solver(c, r, outgrid, maxdiff);
+
+#ifdef STANDALONE_SOLVER
+ solver_recurse_depth--;
+ if (solver_show_working) {
+ printf("%*sretracting %d at (%d,%d)\n",
+ solver_recurse_depth*4, "", list[i], x, y);
+ }
+#endif
+
+ /*
+ * If we have our first solution, copy it into the
+ * grid we will return.
+ */
+ if (diff == DIFF_IMPOSSIBLE && ret != DIFF_IMPOSSIBLE)
+ memcpy(grid, outgrid, cr*cr);
+
+ 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);
+ }
+
+ } else {
+ /*
+ * We're forbidden to use recursion, so we just see whether
+ * our grid is fully solved, and return DIFF_IMPOSSIBLE
+ * otherwise.
+ */
+ for (y = 0; y < cr; y++)
+ for (x = 0; x < cr; x++)
+ if (!grid[y*cr+x])
+ diff = DIFF_IMPOSSIBLE;
+ }
+
+ got_result:;
+
+#ifdef STANDALONE_SOLVER
+ if (solver_show_working)
+ printf("%*s%s found\n",
+ solver_recurse_depth*4, "",
+ diff == DIFF_IMPOSSIBLE ? "no solution" :
+ diff == DIFF_AMBIGUOUS ? "multiple solutions" :
+ "one solution");
+#endif
+
+ sfree(usage->cube);
+ sfree(usage->row);
+ sfree(usage->col);
+ sfree(usage->blk);
+ sfree(usage);
+
+ solver_free_scratch(scratch);
+
+ return diff;
+}
+
+/* ----------------------------------------------------------------------
+ * End of solver code.
+ */
+
+/* ----------------------------------------------------------------------
+ * Solo filled-grid generator.
+ *
+ * This grid generator works by essentially trying to solve a grid
+ * starting from no clues, and not worrying that there's more than
+ * one possible solution. Unfortunately, it isn't computationally
+ * feasible to do this by calling the above solver with an empty
+ * grid, because that one needs to allocate a lot of scratch space
+ * at every recursion level. Instead, I have a much simpler
+ * algorithm which I shamelessly copied from a Python solver
+ * written by Andrew Wilkinson (which is GPLed, but I've reused
+ * only ideas and no code). It mostly just does the obvious
+ * recursive thing: pick an empty square, put one of the possible
+ * digits in it, recurse until all squares are filled, backtrack
+ * and change some choices if necessary.
+ *
+ * The clever bit is that every time it chooses which square to
+ * fill in next, it does so by counting the number of _possible_
+ * numbers that can go in each square, and it prioritises so that
+ * it picks a square with the _lowest_ number of possibilities. The
+ * idea is that filling in lots of the obvious bits (particularly
+ * any squares with only one possibility) will cut down on the list
+ * of possibilities for other squares and hence reduce the enormous
+ * search space as much as possible as early as possible.
+ */
+
+/*
+ * Internal data structure used in gridgen to keep track of
+ * progress.
+ */
+struct gridgen_coord { int x, y, r; };
+struct gridgen_usage {
+ int c, r, cr; /* cr == c*r */
+ /* grid is a copy of the input grid, modified as we go along */
+ digit *grid;
+ /* row[y*cr+n-1] TRUE if digit n has been placed in row y */
+ unsigned char *row;
+ /* col[x*cr+n-1] TRUE if digit n has been placed in row x */
+ unsigned char *col;
+ /* blk[(y*c+x)*cr+n-1] TRUE if digit n has been placed in block (x,y) */
+ unsigned char *blk;
+ /* This lists all the empty spaces remaining in the grid. */
+ struct gridgen_coord *spaces;
+ int nspaces;
+ /* If we need randomisation in the solve, this is our random state. */
+ random_state *rs;
+};
+
+/*
+ * The real recursive step in the generating function.
+ */
+static int gridgen_real(struct gridgen_usage *usage, digit *grid)
+{
+ int c = usage->c, r = usage->r, cr = usage->cr;
+ int i, j, n, sx, sy, bestm, bestr, ret;
+ int *digits;
+
+ /*
+ * Firstly, check for completion! If there are no spaces left
+ * in the grid, we have a solution.
+ */
+ if (usage->nspaces == 0) {
+ memcpy(grid, usage->grid, cr * cr);
+ return TRUE;
+ }
+
+ /*
+ * Otherwise, there must be at least one space. Find the most
+ * constrained space, using the `r' field as a tie-breaker.
+ */
+ bestm = cr+1; /* so that any space will beat it */
+ bestr = 0;
+ i = sx = sy = -1;
+ for (j = 0; j < usage->nspaces; j++) {
+ int x = usage->spaces[j].x, y = usage->spaces[j].y;
+ int m;
+
+ /*
+ * Find the number of digits that could go in this space.
+ */
+ m = 0;
+ for (n = 0; n < cr; n++)
+ if (!usage->row[y*cr+n] && !usage->col[x*cr+n] &&
+ !usage->blk[((y/c)*c+(x/r))*cr+n])
+ m++;
+
+ if (m < bestm || (m == bestm && usage->spaces[j].r < bestr)) {
+ bestm = m;
+ bestr = usage->spaces[j].r;
+ sx = x;
+ sy = y;
+ i = j;
+ }
+ }
+
+ /*
+ * Swap that square into the final place in the spaces array,
+ * so that decrementing nspaces will remove it from the list.
+ */
+ if (i != usage->nspaces-1) {
+ struct gridgen_coord t;
+ t = usage->spaces[usage->nspaces-1];
+ usage->spaces[usage->nspaces-1] = usage->spaces[i];
+ usage->spaces[i] = t;
+ }
+
+ /*
+ * Now we've decided which square to start our recursion at,
+ * simply go through all possible values, shuffling them
+ * randomly first if necessary.
+ */
+ digits = snewn(bestm, int);
+ j = 0;
+ for (n = 0; n < cr; n++)
+ if (!usage->row[sy*cr+n] && !usage->col[sx*cr+n] &&
+ !usage->blk[((sy/c)*c+(sx/r))*cr+n]) {
+ digits[j++] = n+1;
+ }
+
+ if (usage->rs)
+ shuffle(digits, j, sizeof(*digits), usage->rs);
+
+ /* And finally, go through the digit list and actually recurse. */
+ ret = FALSE;
+ for (i = 0; i < j; i++) {
+ n = digits[i];
+
+ /* Update the usage structure to reflect the placing of this digit. */
+ usage->row[sy*cr+n-1] = usage->col[sx*cr+n-1] =
+ usage->blk[((sy/c)*c+(sx/r))*cr+n-1] = TRUE;
+ usage->grid[sy*cr+sx] = n;
+ usage->nspaces--;
+
+ /* Call the solver recursively. Stop when we find a solution. */
+ if (gridgen_real(usage, grid))
+ ret = TRUE;
+
+ /* Revert the usage structure. */
+ usage->row[sy*cr+n-1] = usage->col[sx*cr+n-1] =
+ usage->blk[((sy/c)*c+(sx/r))*cr+n-1] = FALSE;
+ usage->grid[sy*cr+sx] = 0;
+ usage->nspaces++;
+
+ if (ret)
+ break;
+ }
+
+ sfree(digits);
+ return ret;
+}
+
+/*
+ * Entry point to generator. You give it dimensions and a starting
+ * grid, which is simply an array of cr*cr digits.
+ */
+static void gridgen(int c, int r, digit *grid, random_state *rs)
+{
+ struct gridgen_usage *usage;
+ int x, y, cr = c*r;
+
+ /*
+ * Clear the grid to start with.
+ */
+ memset(grid, 0, cr*cr);
+
+ /*
+ * Create a gridgen_usage structure.
+ */
+ usage = snew(struct gridgen_usage);
+
+ usage->c = c;
+ usage->r = r;
+ usage->cr = cr;
+
+ usage->grid = snewn(cr * cr, digit);
+ memcpy(usage->grid, grid, cr * cr);