Make generator always produce possible puzzles
This commit is contained in:
parent
5ae7a18227
commit
d6b9f3e2e5
74
sud.c
74
sud.c
@ -22,12 +22,15 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define FILLP_RCP 3
|
#define REM_PROB_NUMER 2
|
||||||
|
#define REM_PROB_DENOM 3
|
||||||
|
|
||||||
|
#define MAX_FILL_ATTEMPTS 32
|
||||||
|
|
||||||
static void initposs(struct sudoku *sud)
|
static void initposs(struct sudoku *sud)
|
||||||
{
|
{
|
||||||
for (unsigned r = 0; r < SIDELEN; ++r) {
|
for (unsigned r = 0; r < NDIGITS; ++r) {
|
||||||
for (unsigned c = 0; c < SIDELEN; ++c) {
|
for (unsigned c = 0; c < NDIGITS; ++c) {
|
||||||
sud->cells[r][c].det = false;
|
sud->cells[r][c].det = false;
|
||||||
for (unsigned i = 0; i < NDIGITS; ++i)
|
for (unsigned i = 0; i < NDIGITS; ++i)
|
||||||
sud->cells[r][c].pvals[i] = true;
|
sud->cells[r][c].pvals[i] = true;
|
||||||
@ -35,22 +38,67 @@ static void initposs(struct sudoku *sud)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void clear(struct sudoku *sud, unsigned r, unsigned c)
|
||||||
|
{
|
||||||
|
unsigned tr, tc, val;
|
||||||
|
|
||||||
|
/* Set undetermined with all possible values */
|
||||||
|
sud->cells[r][c].det = false;
|
||||||
|
for (unsigned i = 0; i < NDIGITS; ++i)
|
||||||
|
sud->cells[r][c].pvals[i] = true;
|
||||||
|
|
||||||
|
/* Update cell's possible values based off column. */
|
||||||
|
for (tr = 0; tr < NDIGITS; ++tr) {
|
||||||
|
if (tr == r || !sud->cells[tr][c].det)
|
||||||
|
continue;
|
||||||
|
val = sud->cells[tr][c].val;
|
||||||
|
sud->cells[r][c].pvals[val] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update possible values of cells in same row. */
|
||||||
|
for (tc = 0; tc < NDIGITS; ++tc) {
|
||||||
|
if (tc == c || !sud->cells[r][tc].det)
|
||||||
|
continue;
|
||||||
|
val = sud->cells[r][tc].val;
|
||||||
|
sud->cells[r][c].pvals[val] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update possible values of cells in same segment. */
|
||||||
|
const unsigned segr0 = SEGLEN * (r / SEGLEN);
|
||||||
|
const unsigned segc0 = SEGLEN * (c / SEGLEN);
|
||||||
|
for (tr = segr0; tr < segr0 + SEGLEN; ++tr) {
|
||||||
|
for (tc = segc0; tc < segc0 + SEGLEN; ++tc) {
|
||||||
|
if ((tr == r && tc == c) || !sud->cells[tr][tc].det)
|
||||||
|
continue;
|
||||||
|
val = sud->cells[tr][tc].val;
|
||||||
|
sud->cells[r][c].pvals[val] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void gen(struct sudoku *sud)
|
void gen(struct sudoku *sud)
|
||||||
{
|
{
|
||||||
|
/* Generate a completed sudoku. */
|
||||||
|
retry:
|
||||||
initposs(sud);
|
initposs(sud);
|
||||||
|
for (unsigned r = 0; r < NDIGITS; ++r) {
|
||||||
for (unsigned r = 0; r < SIDELEN; ++r) {
|
for (unsigned c = 0; c < NDIGITS; ++c) {
|
||||||
for (unsigned c = 0; c < SIDELEN; ++c) {
|
|
||||||
if (rand() % FILLP_RCP == 0) {
|
|
||||||
assert(!sud->cells[r][c].det);
|
|
||||||
|
|
||||||
unsigned val, n = 0;
|
unsigned val, n = 0;
|
||||||
do {
|
do {
|
||||||
val = (unsigned)(rand() % NDIGITS);
|
val = (unsigned)(rand() % NDIGITS);
|
||||||
++n;
|
++n;
|
||||||
|
if (n >= MAX_FILL_ATTEMPTS)
|
||||||
|
goto retry;
|
||||||
} while (update(sud, r, c, val) == NOT_ALLOWED);
|
} while (update(sud, r, c, val) == NOT_ALLOWED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Remove cells with probability of `1 / REMOVE_PROB_RECIP`. */
|
||||||
|
for (unsigned r = 0; r < NDIGITS; ++r) {
|
||||||
|
for (unsigned c = 0; c < NDIGITS; ++c) {
|
||||||
|
if (rand() % REM_PROB_DENOM < REM_PROB_NUMER)
|
||||||
|
clear(sud, r, c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,14 +114,14 @@ enum update_res update(struct sudoku *sud, unsigned r, unsigned c, unsigned val)
|
|||||||
return NOT_ALLOWED;
|
return NOT_ALLOWED;
|
||||||
|
|
||||||
/* Update possible values of cells in same column. */
|
/* Update possible values of cells in same column. */
|
||||||
for (tr = 0; tr < SIDELEN; ++tr) {
|
for (tr = 0; tr < NDIGITS; ++tr) {
|
||||||
if (tr == r || sud->cells[tr][c].det)
|
if (tr == r || sud->cells[tr][c].det)
|
||||||
continue;
|
continue;
|
||||||
sud->cells[tr][c].pvals[val] = false;
|
sud->cells[tr][c].pvals[val] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Update possible values of cells in same row. */
|
/* Update possible values of cells in same row. */
|
||||||
for (tc = 0; tc < SIDELEN; ++tc) {
|
for (tc = 0; tc < NDIGITS; ++tc) {
|
||||||
if (tc == c || sud->cells[r][tc].det)
|
if (tc == c || sud->cells[r][tc].det)
|
||||||
continue;
|
continue;
|
||||||
sud->cells[r][tc].pvals[val] = false;
|
sud->cells[r][tc].pvals[val] = false;
|
||||||
@ -99,7 +147,7 @@ enum update_res update(struct sudoku *sud, unsigned r, unsigned c, unsigned val)
|
|||||||
|
|
||||||
void print(const struct sudoku *sud)
|
void print(const struct sudoku *sud)
|
||||||
{
|
{
|
||||||
for (unsigned r = 0; r < SIDELEN; ++r) {
|
for (unsigned r = 0; r < NDIGITS; ++r) {
|
||||||
/*
|
/*
|
||||||
* Print horizontal divider if on a segment boundary (but not
|
* Print horizontal divider if on a segment boundary (but not
|
||||||
* at the start).
|
* at the start).
|
||||||
@ -107,7 +155,7 @@ void print(const struct sudoku *sud)
|
|||||||
if (r != 0 && r % SEGLEN == 0)
|
if (r != 0 && r % SEGLEN == 0)
|
||||||
puts("------+-------+------");
|
puts("------+-------+------");
|
||||||
|
|
||||||
for (unsigned c = 0; c < SIDELEN; ++c) {
|
for (unsigned c = 0; c < NDIGITS; ++c) {
|
||||||
/*
|
/*
|
||||||
* Print vertical divider if on a segment boundary (but
|
* Print vertical divider if on a segment boundary (but
|
||||||
* not at the start).
|
* not at the start).
|
||||||
|
3
sud.h
3
sud.h
@ -21,7 +21,6 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define SIDELEN 9
|
|
||||||
#define SEGLEN 3
|
#define SEGLEN 3
|
||||||
#define NDIGITS 9
|
#define NDIGITS 9
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ struct cellstate {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct sudoku {
|
struct sudoku {
|
||||||
struct cellstate cells[SIDELEN][SIDELEN];
|
struct cellstate cells[NDIGITS][NDIGITS];
|
||||||
};
|
};
|
||||||
|
|
||||||
enum update_res { NOT_ALLOWED, ALREADY_DET, OK };
|
enum update_res { NOT_ALLOWED, ALREADY_DET, OK };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user