+ solver_place(usage, x, y, v);
+
+#ifdef STANDALONE_SOLVER
+ if (solver_show_working) {
+ printf("%*s placing %d at (%d,%d)\n",
+ solver_recurse_depth*4, "killer single-square cage",
+ v, 1 + x%cr, 1 + x/cr);
+ }
+#endif
+ changed = TRUE;
+ }
+ }
+
+ if (changed) {
+ kdiff = max(kdiff, DIFF_KSINGLE);
+ goto cont;
+ }
+ }
+ if (dlev->maxkdiff >= DIFF_KINTERSECT && usage->kclues != NULL) {
+ int changed = FALSE;
+ /*
+ * Now, create the extra_cages information. Every full region
+ * (row, column, or block) has the same sum total (45 for 3x3
+ * puzzles. After we try to cover these regions with cages that
+ * lie entirely within them, any squares that remain must bring
+ * the total to this known value, and so they form additional
+ * cages which aren't immediately evident in the displayed form
+ * of the puzzle.
+ */
+ usage->extra_cages->nr_blocks = 0;
+ for (i = 0; i < 3; i++) {
+ for (n = 0; n < cr; n++) {
+ int *region = usage->regions + cr*n*3 + i*cr;
+ int sum = cr * (cr + 1) / 2;
+ int nsquares = cr;
+ int filtered;
+ int n_extra = usage->extra_cages->nr_blocks;
+ int *extra_list = usage->extra_cages->blocks[n_extra];
+ memcpy(extra_list, region, cr * sizeof *extra_list);
+
+ nsquares = filter_whole_cages(usage, extra_list, nsquares, &filtered);
+ sum -= filtered;
+ if (nsquares == cr || nsquares == 0)
+ continue;
+ if (dlev->maxdiff >= DIFF_RECURSIVE) {
+ if (sum <= 0) {
+ dlev->diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ }
+ }
+ assert(sum > 0);
+
+ if (nsquares == 1) {
+ if (sum > cr) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ }
+ x = extra_list[0] % cr;
+ y = extra_list[0] / cr;
+ if (!cube(x, y, sum)) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ }
+ solver_place(usage, x, y, sum);
+ changed = TRUE;
+#ifdef STANDALONE_SOLVER
+ if (solver_show_working) {
+ printf("%*s placing %d at (%d,%d)\n",
+ solver_recurse_depth*4, "killer single-square deduced cage",
+ sum, 1 + x, 1 + y);
+ }
+#endif
+ }
+
+ b = usage->kblocks->whichblock[extra_list[0]];
+ for (x = 1; x < nsquares; x++)
+ if (usage->kblocks->whichblock[extra_list[x]] != b)
+ break;
+ if (x == nsquares) {
+ assert(usage->kblocks->nr_squares[b] > nsquares);
+ split_block(usage->kblocks, extra_list, nsquares);
+ assert(usage->kblocks->nr_squares[usage->kblocks->nr_blocks - 1] == nsquares);
+ usage->kclues[usage->kblocks->nr_blocks - 1] = sum;
+ usage->kclues[b] -= sum;
+ } else {
+ usage->extra_cages->nr_squares[n_extra] = nsquares;
+ usage->extra_cages->nr_blocks++;
+ usage->extra_clues[n_extra] = sum;
+ }
+ }
+ }
+ if (changed) {
+ kdiff = max(kdiff, DIFF_KINTERSECT);
+ goto cont;
+ }
+ }
+
+ /*
+ * Another simple killer-type elimination. For every square in a
+ * cage, find the minimum and maximum possible sums of all the
+ * other squares in the same cage, and rule out possibilities
+ * for the given square based on whether they are guaranteed to
+ * cause the sum to be either too high or too low.
+ * This is a special case of trying all possible sums across a
+ * region, which is a recursive algorithm. We should probably
+ * implement it for a higher difficulty level.
+ */
+ if (dlev->maxkdiff >= DIFF_KMINMAX && usage->kclues != NULL) {
+ int changed = FALSE;
+ for (b = 0; b < usage->kblocks->nr_blocks; b++) {
+ int ret = solver_killer_minmax(usage, usage->kblocks,
+ usage->kclues, b
+#ifdef STANDALONE_SOLVER
+ , ""
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0)
+ changed = TRUE;
+ }
+ for (b = 0; b < usage->extra_cages->nr_blocks; b++) {
+ int ret = solver_killer_minmax(usage, usage->extra_cages,
+ usage->extra_clues, b
+#ifdef STANDALONE_SOLVER
+ , "using deduced cages"
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0)
+ changed = TRUE;
+ }
+ if (changed) {
+ kdiff = max(kdiff, DIFF_KMINMAX);
+ goto cont;
+ }
+ }
+
+ /*
+ * Try to use knowledge of which numbers can be used to generate
+ * a given sum.
+ * This can only be used if a cage lies entirely within a region.
+ */
+ if (dlev->maxkdiff >= DIFF_KSUMS && usage->kclues != NULL) {
+ int changed = FALSE;
+
+ for (b = 0; b < usage->kblocks->nr_blocks; b++) {
+ int ret = solver_killer_sums(usage, b, usage->kblocks,
+ usage->kclues[b], TRUE
+#ifdef STANDALONE_SOLVER
+ , "regular clues"
+#endif
+ );
+ if (ret > 0) {
+ changed = TRUE;
+ kdiff = max(kdiff, DIFF_KSUMS);
+ } else if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ }
+ }
+
+ for (b = 0; b < usage->extra_cages->nr_blocks; b++) {
+ int ret = solver_killer_sums(usage, b, usage->extra_cages,
+ usage->extra_clues[b], FALSE
+#ifdef STANDALONE_SOLVER
+ , "deduced clues"
+#endif
+ );
+ if (ret > 0) {
+ changed = TRUE;
+ kdiff = max(kdiff, DIFF_KINTERSECT);
+ } else if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ }
+ }
+
+ if (changed)
+ goto cont;
+ }
+
+ if (dlev->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]) {
+ for (x = 0; x < cr; x++)
+ scratch->indexlist[x] = cubepos(x, y, n);
+ ret = solver_elim(usage, scratch->indexlist
+#ifdef STANDALONE_SOLVER
+ , "positional elimination,"
+ " %d in row %d", n, 1+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]) {
+ for (y = 0; y < cr; y++)
+ scratch->indexlist[y] = cubepos(x, y, n);
+ ret = solver_elim(usage, scratch->indexlist
+#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;
+ }
+ }
+
+ /*
+ * X-diagonal positional elimination.
+ */
+ if (usage->diag) {
+ for (n = 1; n <= cr; n++)
+ if (!usage->diag[n-1]) {
+ for (i = 0; i < cr; i++)
+ scratch->indexlist[i] = cubepos2(diag0(i), n);
+ ret = solver_elim(usage, scratch->indexlist
+#ifdef STANDALONE_SOLVER
+ , "positional elimination,"
+ " %d in \\-diagonal", n
+#endif
+ );
+ if (ret < 0) {
+ diff = DIFF_IMPOSSIBLE;
+ goto got_result;
+ } else if (ret > 0) {
+ diff = max(diff, DIFF_SIMPLE);
+ goto cont;
+ }
+ }
+ for (n = 1; n <= cr; n++)
+ if (!usage->diag[cr+n-1]) {
+ for (i = 0; i < cr; i++)
+ scratch->indexlist[i] = cubepos2(diag1(i), n);
+ ret = solver_elim(usage, scratch->indexlist