Add step to solve() to check if cells have only one possible value

This commit is contained in:
Camden Dixie O'Brien 2022-11-26 01:12:11 +00:00
parent 98a99ac340
commit 38ce9d0abb
2 changed files with 22 additions and 1 deletions

22
solve.c
View File

@ -24,7 +24,7 @@ int solve(struct sudoku *sud)
{
unsigned r, c, n, i, j, val, np, pr, pc;
bool match;
uint16_t valmask;
uint16_t valmask, pvals;
for (n = 0;; ++n) {
match = false;
@ -103,6 +103,26 @@ int solve(struct sudoku *sud)
}
}
/* Check each cell for if it has only one possible value. */
for (r = 0; r < NDIGITS; ++r) {
for (c = 0; c < NDIGITS; ++c) {
if (DET(sud->cells[r][c]))
continue;
pvals = sud->cells[r][c] & PVALSMASK;
for (val = 0; val < NDIGITS; ++val) {
if (pvals & 1) {
if (pvals == 1) {
update(sud, r, c, val);
match = true;
}
break;
}
pvals >>= 1;
}
}
}
/* Exit if no matches. */
if (!match)
return n;

1
sud.h
View File

@ -29,6 +29,7 @@
#define DETMASK 0x8000
#define VALSHIFT 9
#define VALMASK 0x1e00
#define PVALSMASK 0x01ff
#define DET(x) (x & DETMASK)
#define VAL(x) ((x & VALMASK) >> VALSHIFT)