Compare commits

..

10 Commits

Author SHA1 Message Date
Camden Dixie O'Brien
d14677339d Show known-safe tiles in print() 2025-03-22 13:30:25 +00:00
Camden Dixie O'Brien
120d393f12 Fill tiles in search if same in both branches
This gives a ~7% boost in solving ability.
2025-03-22 13:30:25 +00:00
Camden Dixie O'Brien
dd3cabf11b Use queue for tiles to probe
This allows updating and searching until no more inferences can be
made, resulting in a ~5% improvement (also better performance).
2025-03-22 13:30:25 +00:00
Camden Dixie O'Brien
78779c74de Make macro for looping through adjacent tiles 2025-03-22 13:30:25 +00:00
Camden Dixie O'Brien
168cdd8e8e Add simple contradiction-driven search mechanism
Gets an about 6% improvement
2025-03-22 13:30:25 +00:00
Camden Dixie O'Brien
6e9a6810b3 Detect contradictions in update() 2025-03-22 13:30:25 +00:00
Camden Dixie O'Brien
0a99509524 Add SAFE state to indicate un-probed safe tile 2025-03-22 13:30:25 +00:00
Camden Dixie O'Brien
040148f782 Add error checking and better reporting 2025-03-22 13:30:24 +00:00
Camden Dixie O'Brien
02a0dd9763 Refactor to extract update() function 2025-03-22 13:30:08 +00:00
Camden Dixie O'Brien
001b52d714 Set to standard 'beginner' configuration 2025-03-22 13:30:08 +00:00
3 changed files with 297 additions and 88 deletions

291
main.c
View File

@ -7,94 +7,239 @@
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define NRUNS 1000
#define NRUNS 10000
#define MAX_ADJ 8
#define QUEUE_MAX 128
enum { UNKNOWN = 0xfe, KILLER = 0xfd };
typedef enum {
MADE_INFERENCE,
FOUND_NOTHING,
FOUND_CONTRADICTION,
} update_res_t;
typedef struct {
int x, y;
} coord_t;
typedef struct {
coord_t buf[QUEUE_MAX], *start, *end;
} queue_t;
typedef struct {
int mines, unknown;
queue_t queue;
puzz_t field;
} state_t;
static bool empty(const queue_t *queue)
{
return queue->start == queue->end;
}
static void enqueue(queue_t *queue, int x, int y)
{
queue->end->x = x;
queue->end->y = y;
++queue->end;
if (queue->end == queue->buf + QUEUE_MAX)
queue->end = queue->buf;
assert(queue->end != queue->start);
}
static void dequeue(queue_t *queue, int *x_out, int *y_out)
{
assert(queue->start != queue->end);
*x_out = queue->start->x;
*y_out = queue->start->y;
++queue->start;
if (queue->start == queue->buf + QUEUE_MAX)
queue->start = queue->buf;
}
static void init(state_t *state)
{
state->mines = 0;
state->unknown = WIDTH * HEIGHT;
state->queue.start = state->queue.end = state->queue.buf;
memset(state->field, UNKNOWN, sizeof(puzz_t));
}
static void dup(state_t *orig, state_t *copy)
{
memcpy(copy, orig, sizeof(state_t));
const int startpos = orig->queue.start - orig->queue.buf;
const int endpos = orig->queue.end - orig->queue.buf;
copy->queue.start = copy->queue.buf + startpos;
copy->queue.end = copy->queue.buf + endpos;
}
static void setadj(puzz_t field, int x, int y, uint8_t from, uint8_t to)
{
for (int yp = y - 1; yp < y + 2; ++yp) {
for (int xp = x - 1; xp < x + 2; ++xp) {
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|| (xp == x && yp == y))
continue;
field[xp][yp] = field[xp][yp] == from ? to : field[xp][yp];
}
}
FORADJ(x, y, xi, yi)
field[xi][yi] = field[xi][yi] == from ? to : field[xi][yi];
}
static void getadj(puzz_t field, int *x, int *y, uint8_t val)
static update_res_t update(state_t *state)
{
for (int yp = *y - 1; yp < *y + 2; ++yp) {
for (int xp = *x - 1; xp < *x + 2; ++xp) {
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|| (xp == *x && yp == *y))
state->mines = state->unknown = 0;
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
state->mines += state->field[x][y] == MINE ? 1 : 0;
state->unknown += state->field[x][y] == UNKNOWN ? 1 : 0;
}
}
if (state->mines > NMINES || state->mines + state->unknown < NMINES)
return FOUND_CONTRADICTION;
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (state->field[x][y] == UNKNOWN || state->field[x][y] == MINE
|| state->field[x][y] == SAFE)
continue;
if (field[xp][yp] == val) {
*x = xp;
*y = yp;
return;
const int mines = countadj(state->field, x, y, MINE);
if (mines > state->field[x][y])
return FOUND_CONTRADICTION;
const int unknowns = countadj(state->field, x, y, UNKNOWN);
if (unknowns == 0)
continue;
if (mines + unknowns < state->field[x][y])
return FOUND_CONTRADICTION;
if (mines + unknowns == state->field[x][y]) {
setadj(state->field, x, y, UNKNOWN, MINE);
return MADE_INFERENCE;
}
if (mines == state->field[x][y]) {
FORADJ(x, y, xi, yi)
{
if (state->field[xi][yi] == UNKNOWN)
enqueue(&state->queue, xi, yi);
}
setadj(state->field, x, y, UNKNOWN, SAFE);
return MADE_INFERENCE;
}
}
}
assert(false);
return FOUND_NOTHING;
}
static status_t solve(void)
static update_res_t search_at(state_t *state, int x, int y)
{
puzz_t field;
memset(field, UNKNOWN, sizeof(field));
update_res_t res;
status_t status;
int total_mines = 0, total_unknowns = WIDTH * HEIGHT;
state_t with_mine;
dup(state, &with_mine);
with_mine.field[x][y] = MINE;
do {
int x = rand() % WIDTH;
int y = rand() % HEIGHT;
probe:
if (field[x][y] != MINE && (status = probe(x, y, field)) == DEAD) {
field[x][y] = KILLER;
break;
if ((res = update(&with_mine)) == FOUND_CONTRADICTION) {
state->field[x][y] = SAFE;
enqueue(&state->queue, x, y);
return MADE_INFERENCE;
}
} while (res == MADE_INFERENCE);
update:
total_mines = total_unknowns = 0;
for (y = 0; y < HEIGHT; ++y) {
for (x = 0; x < WIDTH; ++x) {
total_mines += field[x][y] == MINE ? 1 : 0;
total_unknowns += field[x][y] == UNKNOWN ? 1 : 0;
state_t with_safe;
dup(state, &with_safe);
with_safe.field[x][y] = SAFE;
do {
if ((res = update(&with_safe)) == FOUND_CONTRADICTION) {
state->field[x][y] = MINE;
return MADE_INFERENCE;
}
} while (res == MADE_INFERENCE);
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (with_mine.field[x][y] == with_safe.field[x][y] &&
state->field[x][y] != with_mine.field[x][y]) {
state->field[x][y] = with_mine.field[x][y];
if (state->field[x][y] == SAFE)
enqueue(&state->queue, x, y);
res = MADE_INFERENCE;
}
}
for (y = 0; y < HEIGHT; ++y) {
for (x = 0; x < WIDTH; ++x) {
if (field[x][y] == UNKNOWN || field[x][y] == MINE)
continue;
}
const int mines = countadj(field, x, y, MINE);
const int unknowns = countadj(field, x, y, UNKNOWN);
if (unknowns == 0)
continue;
return res;
}
if (mines + unknowns == field[x][y]) {
setadj(field, x, y, UNKNOWN, MINE);
goto update;
}
if (mines == field[x][y]) {
getadj(field, &x, &y, UNKNOWN);
goto probe;
}
static update_res_t search(state_t *state)
{
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (state->field[x][y] != UNKNOWN)
continue;
if (countadj(state->field, x, y, UNKNOWN) != MAX_ADJ) {
update_res_t res = search_at(state, x, y);
if (res != FOUND_NOTHING)
return res;
}
}
} while (total_mines < NMINES);
}
return FOUND_NOTHING;
}
return status;
static status_t solve(int *probes_out)
{
state_t state;
init(&state);
enqueue(&state.queue, rand() % WIDTH, rand() % HEIGHT);
int probes = 0;
do {
while (!empty(&state.queue)) {
int x, y;
dequeue(&state.queue, &x, &y);
if (state.field[x][y] != SAFE && state.field[x][y] != UNKNOWN)
continue;
++probes;
if (probe(x, y, state.field) == DEAD) {
*probes_out = probes;
return DEAD;
}
}
update_res_t res;
do {
res = update(&state);
if (res == FOUND_NOTHING)
res = search(&state);
} while (res == MADE_INFERENCE);
if (check(state.field) != OK) {
printf("Incorrect inference!\n");
print(state.field);
printsoln();
return INCORRECT;
}
if (empty(&state.queue)) {
int x, y;
do {
x = rand() % WIDTH;
y = rand() % HEIGHT;
} while (state.field[x][y] == MINE);
enqueue(&state.queue, x, y);
}
} while (state.mines < NMINES || state.unknown > 0);
*probes_out = probes;
return OK;
}
int main(void)
@ -106,14 +251,36 @@ int main(void)
}
srand(tv.tv_usec);
int nsolved = 0;
int nsolved = 0, nfirst = 0, nincorrect = 0, probes;
for (int i = 0; i < NRUNS; ++i) {
gen();
nsolved += solve() == OK ? 1 : 0;
switch (solve(&probes)) {
case DEAD:
if (probes == 1)
++nfirst;
break;
case OK:
++nsolved;
break;
case INCORRECT:
++nincorrect;
break;
}
}
const double prop = (double)nsolved / NRUNS;
printf("Solved %d/%d (%0.1f%%)\n", nsolved, NRUNS, 100 * prop);
if (nincorrect > 0) {
printf(
"Fail: %d incorrect! (%0.1f%%)\n", nincorrect,
(double)nincorrect / NRUNS);
return 1;
}
const double solved_prop = (double)nsolved / NRUNS;
const double first_prop = (double)nfirst / NRUNS;
const double solved_safe_start_prop = (double)nsolved / (NRUNS - nfirst);
printf("Solved %d (%0.1f%%)\n", nsolved, 100 * solved_prop);
printf("%d died on first turn (%0.1f%%)\n", nfirst, 100 * first_prop);
printf("%0.1f%% solved on safe start\n", 100 * solved_safe_start_prop);
return 0;
}

72
puzz.c
View File

@ -38,17 +38,38 @@ void gen(void)
}
}
void print(void)
void print(puzz_t puzz)
{
puts("Solution:");
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x)
putchar(soln[x][y] == MINE ? 'x' : '0' + soln[x][y]);
for (int x = 0; x < WIDTH; ++x) {
char c;
switch (puzz[x][y]) {
case MINE:
c = 'x';
break;
case UNKNOWN:
c = '.';
break;
case SAFE:
c = 's';
break;
default:
c = '0' + puzz[x][y];
break;
}
putchar(c);
}
putchar('\n');
}
putchar('\n');
}
void printsoln(void)
{
puts("Solution:");
print(soln);
}
static void scan_copy(int x, int y, puzz_t out)
{
assert(x >= 0 && x < WIDTH);
@ -60,13 +81,11 @@ static void scan_copy(int x, int y, puzz_t out)
if (soln[x][y] != 0)
return;
for (int yp = y - 1; yp < y + 2; ++yp) {
for (int xp = x - 1; xp < x + 2; ++xp) {
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|| scanned[xp][yp] == YES)
continue;
scan_copy(xp, yp, out);
}
FORADJ(x, y, xi, yi)
{
if (scanned[xi][yi] == YES)
continue;
scan_copy(xi, yi, out);
}
}
@ -84,16 +103,31 @@ status_t probe(int x, int y, puzz_t out)
return OK;
}
status_t check(puzz_t puzz)
{
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
switch (puzz[x][y]) {
case UNKNOWN:
continue;
case SAFE:
if (soln[x][y] == MINE)
return INCORRECT;
break;
default:
if (puzz[x][y] != soln[x][y])
return INCORRECT;
break;
}
}
}
return OK;
}
int countadj(puzz_t field, int x, int y, uint8_t val)
{
int n = 0;
for (int yp = y - 1; yp < y + 2; ++yp) {
for (int xp = x - 1; xp < x + 2; ++xp) {
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|| (xp == x && yp == y))
continue;
n += field[xp][yp] == val ? 1 : 0;
}
}
FORADJ(x, y, xi, yi)
n += field[xi][yi] == val ? 1 : 0;
return n;
}

22
puzz.h
View File

@ -6,20 +6,28 @@
#ifndef PUZZ_H
#define PUZZ_H
#define WIDTH 10
#define HEIGHT 10
#define NMINES 10
#include <stdint.h>
enum { MINE = 0xff };
#define WIDTH 9
#define HEIGHT 9
#define NMINES 10
#define FORADJ(X, Y, XI, YI) \
for (int YI = Y - 1; YI < Y + 2; ++YI) \
for (int XI = X - 1; XI < X + 2; ++XI) \
if (XI >= 0 && XI < WIDTH && YI >= 0 && YI < HEIGHT \
&& (XI != X || YI != Y))
enum { MINE = 0xff, UNKNOWN = 0xfe, SAFE = 0xfd };
typedef uint8_t puzz_t[WIDTH][HEIGHT];
typedef enum { DEAD, OK } status_t;
typedef enum { DEAD, OK, INCORRECT } status_t;
void gen(void);
void print(void);
void print(puzz_t puzz);
void printsoln(void);
status_t probe(int x, int y, puzz_t out);
status_t check(puzz_t puzz);
int countadj(puzz_t field, int x, int y, uint8_t val);
#endif