Make fill proportion a parameter to gen()

This commit is contained in:
Camden Dixie O'Brien 2022-11-23 17:56:12 +00:00
parent 71db045c9f
commit 37e7c6481a
3 changed files with 11 additions and 11 deletions

8
main.c
View File

@ -48,7 +48,9 @@ static uint32_t getseed(void)
return seed; return seed;
} }
static void genpuzzles(struct sudoku puzzles_out[NPUZZLES], bool print_progress) static void genpuzzles(struct sudoku puzzles_out[NPUZZLES],
double fill_prop,
bool print_progress)
{ {
unsigned i, j, bslen = 0; unsigned i, j, bslen = 0;
@ -63,7 +65,7 @@ static void genpuzzles(struct sudoku puzzles_out[NPUZZLES], bool print_progress)
fflush(stdout); fflush(stdout);
} }
gen(&puzzles_out[i]); gen(&puzzles_out[i], fill_prop);
} }
if (print_progress) { if (print_progress) {
@ -118,7 +120,7 @@ int main(void)
srand(seed); srand(seed);
struct sudoku puzzles[NPUZZLES]; struct sudoku puzzles[NPUZZLES];
genpuzzles(puzzles, true); genpuzzles(puzzles, 0.33, true);
struct bench_res res = runbench(puzzles); struct bench_res res = runbench(puzzles);
puts("\n SUMMARY\n ======="); puts("\n SUMMARY\n =======");

9
sud.c
View File

@ -22,9 +22,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define REM_PROB_NUMER 2
#define REM_PROB_DENOM 3
#define MAX_FILL_ATTEMPTS 32 #define MAX_FILL_ATTEMPTS 32
static void initposs(struct sudoku *sud) static void initposs(struct sudoku *sud)
@ -76,7 +73,7 @@ static void clear(struct sudoku *sud, unsigned r, unsigned c)
} }
} }
void gen(struct sudoku *sud) void gen(struct sudoku *sud, double fill_prop)
{ {
/* Generate a completed sudoku. */ /* Generate a completed sudoku. */
retry: retry:
@ -93,10 +90,10 @@ retry:
} }
} }
/* Remove cells with probability of `1 / REMOVE_PROB_RECIP`. */ /* Remove cells with probability of 1 - fill_prop. */
for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned r = 0; r < NDIGITS; ++r) {
for (unsigned c = 0; c < NDIGITS; ++c) { for (unsigned c = 0; c < NDIGITS; ++c) {
if (rand() % REM_PROB_DENOM < REM_PROB_NUMER) if ((double)rand() / (double)RAND_MAX < 1 - fill_prop)
clear(sud, r, c); clear(sud, r, c);
} }
} }

5
sud.h
View File

@ -41,9 +41,10 @@ enum update_res { NOT_ALLOWED, ALREADY_DET, OK };
enum check_res { INCOMPLETE, INCORRECT, SOLVED }; enum check_res { INCOMPLETE, INCORRECT, SOLVED };
/** /**
* Populate the sudoku with some random values. * Populate the sudoku with some random values. The proportion of
* cells that are filled will be approximately `fill_prop`.
*/ */
void gen(struct sudoku *sud); void gen(struct sudoku *sud, double fill_prop);
/** /**
* Attempt to update the cell at row `r`, column `c` to have the value * Attempt to update the cell at row `r`, column `c` to have the value