+ solver_free_scratch(scratch);
+}
+
+/* ----------------------------------------------------------------------
+ * End of solver code.
+ */
+
+/* ----------------------------------------------------------------------
+ * Killer set generator.
+ */
+
+/* ----------------------------------------------------------------------
+ * 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.
+ *
+ * The use of bit sets implies that we support puzzles up to a size of
+ * 32x32 (less if anyone finds a 16-bit machine to compile this on).
+ */
+
+/*
+ * Internal data structure used in gridgen to keep track of
+ * progress.
+ */
+struct gridgen_coord { int x, y, r; };
+struct gridgen_usage {
+ int cr;
+ struct block_structure *blocks, *kblocks;
+ /* grid is a copy of the input grid, modified as we go along */
+ digit *grid;
+ /*
+ * Bitsets. In each of them, bit n is set if digit n has been placed
+ * in the corresponding region. row, col and blk are used for all
+ * puzzles. cge is used only for killer puzzles, and diag is used
+ * only for x-type puzzles.
+ * All of these have cr entries, except diag which only has 2,
+ * and cge, which has as many entries as kblocks.
+ */
+ unsigned int *row, *col, *blk, *cge, *diag;
+ /* 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;
+};
+
+static void gridgen_place(struct gridgen_usage *usage, int x, int y, digit n)
+{
+ unsigned int bit = 1 << n;
+ int cr = usage->cr;
+ usage->row[y] |= bit;
+ usage->col[x] |= bit;
+ usage->blk[usage->blocks->whichblock[y*cr+x]] |= bit;
+ if (usage->cge)
+ usage->cge[usage->kblocks->whichblock[y*cr+x]] |= bit;
+ if (usage->diag) {
+ if (ondiag0(y*cr+x))
+ usage->diag[0] |= bit;
+ if (ondiag1(y*cr+x))
+ usage->diag[1] |= bit;
+ }
+ usage->grid[y*cr+x] = n;
+}
+
+static void gridgen_remove(struct gridgen_usage *usage, int x, int y, digit n)
+{
+ unsigned int mask = ~(1 << n);
+ int cr = usage->cr;
+ usage->row[y] &= mask;
+ usage->col[x] &= mask;
+ usage->blk[usage->blocks->whichblock[y*cr+x]] &= mask;
+ if (usage->cge)
+ usage->cge[usage->kblocks->whichblock[y*cr+x]] &= mask;
+ if (usage->diag) {
+ if (ondiag0(y*cr+x))
+ usage->diag[0] &= mask;
+ if (ondiag1(y*cr+x))
+ usage->diag[1] &= mask;
+ }
+ usage->grid[y*cr+x] = 0;
+}
+
+#define N_SINGLE 32
+
+/*
+ * The real recursive step in the generating function.
+ *
+ * Return values: 1 means solution found, 0 means no solution
+ * found on this branch.
+ */
+static int gridgen_real(struct gridgen_usage *usage, digit *grid, int *steps)
+{
+ int cr = usage->cr;
+ int i, j, n, sx, sy, bestm, bestr, ret;
+ int *digits;
+ unsigned int used;
+
+ /*
+ * Firstly, check for completion! If there are no spaces left
+ * in the grid, we have a solution.
+ */
+ if (usage->nspaces == 0)
+ return TRUE;
+
+ /*
+ * Next, abandon generation if we went over our steps limit.
+ */
+ if (*steps <= 0)
+ return FALSE;
+ (*steps)--;
+
+ /*
+ * 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;
+ used = ~0;
+ i = sx = sy = -1;
+ for (j = 0; j < usage->nspaces; j++) {
+ int x = usage->spaces[j].x, y = usage->spaces[j].y;
+ unsigned int used_xy;
+ int m;
+
+ m = usage->blocks->whichblock[y*cr+x];
+ used_xy = usage->row[y] | usage->col[x] | usage->blk[m];
+ if (usage->cge != NULL)
+ used_xy |= usage->cge[usage->kblocks->whichblock[y*cr+x]];
+ if (usage->cge != NULL)
+ used_xy |= usage->cge[usage->kblocks->whichblock[y*cr+x]];
+ if (usage->diag != NULL) {
+ if (ondiag0(y*cr+x))
+ used_xy |= usage->diag[0];
+ if (ondiag1(y*cr+x))
+ used_xy |= usage->diag[1];
+ }
+
+ /*
+ * Find the number of digits that could go in this space.
+ */
+ m = 0;
+ for (n = 1; n <= cr; n++) {
+ unsigned int bit = 1 << n;
+ if ((used_xy & bit) == 0)
+ m++;
+ }
+ if (m < bestm || (m == bestm && usage->spaces[j].r < bestr)) {
+ bestm = m;
+ bestr = usage->spaces[j].r;
+ sx = x;
+ sy = y;
+ i = j;
+ used = used_xy;
+ }
+ }
+
+ /*
+ * 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 = 1; n <= cr; n++) {
+ unsigned int bit = 1 << n;
+
+ if ((used & bit) == 0)
+ digits[j++] = n;
+ }
+
+ 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. */
+ gridgen_place(usage, sx, sy, n);
+ usage->nspaces--;
+
+ /* Call the solver recursively. Stop when we find a solution. */
+ if (gridgen_real(usage, grid, steps)) {
+ ret = TRUE;
+ break;
+ }
+
+ /* Revert the usage structure. */
+ gridgen_remove(usage, sx, sy, n);
+ usage->nspaces++;
+ }
+
+ sfree(digits);
+ return ret;
+}
+
+/*
+ * Entry point to generator. You give it parameters and a starting
+ * grid, which is simply an array of cr*cr digits.
+ */
+static int gridgen(int cr, struct block_structure *blocks,
+ struct block_structure *kblocks, int xtype,
+ digit *grid, random_state *rs, int maxsteps)
+{
+ struct gridgen_usage *usage;
+ int x, y, ret;
+
+ /*
+ * Clear the grid to start with.
+ */
+ memset(grid, 0, cr*cr);
+
+ /*
+ * Create a gridgen_usage structure.
+ */
+ usage = snew(struct gridgen_usage);
+
+ usage->cr = cr;
+ usage->blocks = blocks;
+
+ usage->grid = grid;
+
+ usage->row = snewn(cr, unsigned int);
+ usage->col = snewn(cr, unsigned int);
+ usage->blk = snewn(cr, unsigned int);
+ if (kblocks != NULL) {
+ usage->kblocks = kblocks;
+ usage->cge = snewn(usage->kblocks->nr_blocks, unsigned int);
+ memset(usage->cge, FALSE, kblocks->nr_blocks * sizeof *usage->cge);
+ } else {
+ usage->cge = NULL;
+ }
+
+ memset(usage->row, 0, cr * sizeof *usage->row);
+ memset(usage->col, 0, cr * sizeof *usage->col);
+ memset(usage->blk, 0, cr * sizeof *usage->blk);
+
+ if (xtype) {
+ usage->diag = snewn(2, unsigned int);
+ memset(usage->diag, 0, 2 * sizeof *usage->diag);
+ } else {
+ usage->diag = NULL;
+ }
+
+ /*
+ * Begin by filling in the whole top row with randomly chosen
+ * numbers. This cannot introduce any bias or restriction on
+ * the available grids, since we already know those numbers
+ * are all distinct so all we're doing is choosing their
+ * labels.
+ */