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;
|
unsigned r, c, n, i, j, val, np, pr, pc;
|
||||||
bool match;
|
bool match;
|
||||||
uint16_t valmask;
|
uint16_t valmask, pvals;
|
||||||
|
|
||||||
for (n = 0;; ++n) {
|
for (n = 0;; ++n) {
|
||||||
match = false;
|
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. */
|
/* Exit if no matches. */
|
||||||
if (!match)
|
if (!match)
|
||||||
return n;
|
return n;
|
||||||
|
1
sud.h
1
sud.h
@ -29,6 +29,7 @@
|
|||||||
#define DETMASK 0x8000
|
#define DETMASK 0x8000
|
||||||
#define VALSHIFT 9
|
#define VALSHIFT 9
|
||||||
#define VALMASK 0x1e00
|
#define VALMASK 0x1e00
|
||||||
|
#define PVALSMASK 0x01ff
|
||||||
|
|
||||||
#define DET(x) (x & DETMASK)
|
#define DET(x) (x & DETMASK)
|
||||||
#define VAL(x) ((x & VALMASK) >> VALSHIFT)
|
#define VAL(x) ((x & VALMASK) >> VALSHIFT)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user