Add steps to test columns and segments back

Turns out these are needed for some of them... I guess the way I was
generating them was somehow avoiding tricky ones being created?
This commit is contained in:
Camden Dixie O'Brien 2022-11-23 21:41:59 +00:00
parent 5622e2ec9f
commit 0ca01985cd

45
solve.c
View File

@ -22,7 +22,7 @@
int solve(struct sudoku *sud) int solve(struct sudoku *sud)
{ {
unsigned r, c, n, val, np, pr, pc; unsigned r, c, n, i, j, val, np, pr, pc;
bool match; bool match;
for (n = 0;; ++n) { for (n = 0;; ++n) {
@ -41,7 +41,50 @@ int solve(struct sudoku *sud)
break; break;
} }
} }
if (np == 1) {
if (update(sud, pr, pc, val) != OK)
return -1;
match = true;
}
}
}
/* Check if only one place a value can go on each column. */
for (c = 0; c < NDIGITS; ++c) {
for (val = 0; val < NDIGITS; ++val) {
np = 0;
for (r = 0; r < NDIGITS; ++r) {
if (!sud->cells[r][c].det && sud->cells[r][c].pvals[val]) {
pr = r;
pc = c;
++np;
if (np > 1)
break;
}
}
if (np == 1) {
if (update(sud, pr, pc, val) != OK)
return -1;
match = true;
}
}
}
/* Check if only one place a value can go in each segment. */
for (i = 0; i < NDIGITS; ++i) {
for (val = 0; val < NDIGITS; ++val) {
np = 0;
for (j = 0; j < NDIGITS; ++j) {
r = SEGLEN * (i / SEGLEN) + j / SEGLEN;
c = SEGLEN * (i % SEGLEN) + j % SEGLEN;
if (!sud->cells[r][c].det && sud->cells[r][c].pvals[val]) {
pr = r;
pc = c;
++np;
if (np > 1)
break;
}
}
if (np == 1) { if (np == 1) {
if (update(sud, pr, pc, val) != OK) if (update(sud, pr, pc, val) != OK)
return -1; return -1;