Compare commits
10 Commits
7f2a9b79ae
...
d14677339d
Author | SHA1 | Date | |
---|---|---|---|
|
d14677339d | ||
|
120d393f12 | ||
|
dd3cabf11b | ||
|
78779c74de | ||
|
168cdd8e8e | ||
|
6e9a6810b3 | ||
|
0a99509524 | ||
|
040148f782 | ||
|
02a0dd9763 | ||
|
001b52d714 |
291
main.c
291
main.c
@ -7,94 +7,239 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/time.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)
|
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) {
|
FORADJ(x, y, xi, yi)
|
||||||
for (int xp = x - 1; xp < x + 2; ++xp) {
|
field[xi][yi] = field[xi][yi] == from ? to : field[xi][yi];
|
||||||
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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
state->mines = state->unknown = 0;
|
||||||
for (int xp = *x - 1; xp < *x + 2; ++xp) {
|
|
||||||
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|
for (int y = 0; y < HEIGHT; ++y) {
|
||||||
|| (xp == *x && yp == *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;
|
continue;
|
||||||
if (field[xp][yp] == val) {
|
|
||||||
*x = xp;
|
const int mines = countadj(state->field, x, y, MINE);
|
||||||
*y = yp;
|
if (mines > state->field[x][y])
|
||||||
return;
|
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;
|
update_res_t res;
|
||||||
memset(field, UNKNOWN, sizeof(field));
|
|
||||||
|
|
||||||
status_t status;
|
state_t with_mine;
|
||||||
int total_mines = 0, total_unknowns = WIDTH * HEIGHT;
|
dup(state, &with_mine);
|
||||||
|
with_mine.field[x][y] = MINE;
|
||||||
do {
|
do {
|
||||||
int x = rand() % WIDTH;
|
if ((res = update(&with_mine)) == FOUND_CONTRADICTION) {
|
||||||
int y = rand() % HEIGHT;
|
state->field[x][y] = SAFE;
|
||||||
|
enqueue(&state->queue, x, y);
|
||||||
probe:
|
return MADE_INFERENCE;
|
||||||
if (field[x][y] != MINE && (status = probe(x, y, field)) == DEAD) {
|
|
||||||
field[x][y] = KILLER;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} while (res == MADE_INFERENCE);
|
||||||
|
|
||||||
update:
|
state_t with_safe;
|
||||||
total_mines = total_unknowns = 0;
|
dup(state, &with_safe);
|
||||||
for (y = 0; y < HEIGHT; ++y) {
|
with_safe.field[x][y] = SAFE;
|
||||||
for (x = 0; x < WIDTH; ++x) {
|
do {
|
||||||
total_mines += field[x][y] == MINE ? 1 : 0;
|
if ((res = update(&with_safe)) == FOUND_CONTRADICTION) {
|
||||||
total_unknowns += field[x][y] == UNKNOWN ? 1 : 0;
|
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);
|
return res;
|
||||||
const int unknowns = countadj(field, x, y, UNKNOWN);
|
}
|
||||||
if (unknowns == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (mines + unknowns == field[x][y]) {
|
static update_res_t search(state_t *state)
|
||||||
setadj(field, x, y, UNKNOWN, MINE);
|
{
|
||||||
goto update;
|
for (int y = 0; y < HEIGHT; ++y) {
|
||||||
}
|
for (int x = 0; x < WIDTH; ++x) {
|
||||||
|
if (state->field[x][y] != UNKNOWN)
|
||||||
if (mines == field[x][y]) {
|
continue;
|
||||||
getadj(field, &x, &y, UNKNOWN);
|
if (countadj(state->field, x, y, UNKNOWN) != MAX_ADJ) {
|
||||||
goto probe;
|
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)
|
int main(void)
|
||||||
@ -106,14 +251,36 @@ int main(void)
|
|||||||
}
|
}
|
||||||
srand(tv.tv_usec);
|
srand(tv.tv_usec);
|
||||||
|
|
||||||
int nsolved = 0;
|
int nsolved = 0, nfirst = 0, nincorrect = 0, probes;
|
||||||
for (int i = 0; i < NRUNS; ++i) {
|
for (int i = 0; i < NRUNS; ++i) {
|
||||||
gen();
|
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;
|
if (nincorrect > 0) {
|
||||||
printf("Solved %d/%d (%0.1f%%)\n", nsolved, NRUNS, 100 * prop);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
72
puzz.c
72
puzz.c
@ -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 y = 0; y < HEIGHT; ++y) {
|
||||||
for (int x = 0; x < WIDTH; ++x)
|
for (int x = 0; x < WIDTH; ++x) {
|
||||||
putchar(soln[x][y] == MINE ? 'x' : '0' + soln[x][y]);
|
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');
|
||||||
}
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void printsoln(void)
|
||||||
|
{
|
||||||
|
puts("Solution:");
|
||||||
|
print(soln);
|
||||||
|
}
|
||||||
|
|
||||||
static void scan_copy(int x, int y, puzz_t out)
|
static void scan_copy(int x, int y, puzz_t out)
|
||||||
{
|
{
|
||||||
assert(x >= 0 && x < WIDTH);
|
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)
|
if (soln[x][y] != 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (int yp = y - 1; yp < y + 2; ++yp) {
|
FORADJ(x, y, xi, yi)
|
||||||
for (int xp = x - 1; xp < x + 2; ++xp) {
|
{
|
||||||
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|
if (scanned[xi][yi] == YES)
|
||||||
|| scanned[xp][yp] == YES)
|
continue;
|
||||||
continue;
|
scan_copy(xi, yi, out);
|
||||||
scan_copy(xp, yp, out);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,16 +103,31 @@ status_t probe(int x, int y, puzz_t out)
|
|||||||
return OK;
|
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 countadj(puzz_t field, int x, int y, uint8_t val)
|
||||||
{
|
{
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (int yp = y - 1; yp < y + 2; ++yp) {
|
FORADJ(x, y, xi, yi)
|
||||||
for (int xp = x - 1; xp < x + 2; ++xp) {
|
n += field[xi][yi] == val ? 1 : 0;
|
||||||
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|
|
||||||
|| (xp == x && yp == y))
|
|
||||||
continue;
|
|
||||||
n += field[xp][yp] == val ? 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
22
puzz.h
22
puzz.h
@ -6,20 +6,28 @@
|
|||||||
#ifndef PUZZ_H
|
#ifndef PUZZ_H
|
||||||
#define PUZZ_H
|
#define PUZZ_H
|
||||||
|
|
||||||
#define WIDTH 10
|
|
||||||
#define HEIGHT 10
|
|
||||||
#define NMINES 10
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#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 uint8_t puzz_t[WIDTH][HEIGHT];
|
||||||
typedef enum { DEAD, OK } status_t;
|
typedef enum { DEAD, OK, INCORRECT } status_t;
|
||||||
|
|
||||||
void gen(void);
|
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 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);
|
int countadj(puzz_t field, int x, int y, uint8_t val);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user