Add step to solve() to check if cells have only one possible value
This commit is contained in:
parent
98a99ac340
commit
38ce9d0abb
22
solve.c
22
solve.c
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user