Fill tiles in search if same in both branches

This gives a ~7% boost in solving ability.
This commit is contained in:
Camden Dixie O'Brien 2025-03-22 13:33:38 +00:00
parent c5687d5b06
commit a4402c0975

31
main.c
View File

@ -17,8 +17,7 @@
#define QUEUE_MAX 128
typedef enum {
FOUND_MINE,
FOUND_SAFE,
MADE_INFERENCE,
FOUND_NOTHING,
FOUND_CONTRADICTION,
} update_res_t;
@ -120,7 +119,7 @@ static update_res_t update(state_t *state)
if (mines + unknowns == state->field[x][y]) {
setadj(state->field, x, y, UNKNOWN, MINE);
return FOUND_MINE;
return MADE_INFERENCE;
}
if (mines == state->field[x][y]) {
@ -130,7 +129,7 @@ static update_res_t update(state_t *state)
enqueue(&state->queue, xi, yi);
}
setadj(state->field, x, y, UNKNOWN, SAFE);
return FOUND_SAFE;
return MADE_INFERENCE;
}
}
}
@ -149,9 +148,9 @@ static update_res_t search_at(state_t *state, int x, int y)
if ((res = update(&with_mine)) == FOUND_CONTRADICTION) {
state->field[x][y] = SAFE;
enqueue(&state->queue, x, y);
return FOUND_SAFE;
return MADE_INFERENCE;
}
} while (res != FOUND_NOTHING);
} while (res == MADE_INFERENCE);
state_t with_safe;
dup(state, &with_safe);
@ -159,11 +158,23 @@ static update_res_t search_at(state_t *state, int x, int y)
do {
if ((res = update(&with_safe)) == FOUND_CONTRADICTION) {
state->field[x][y] = MINE;
return FOUND_MINE;
return MADE_INFERENCE;
}
} while (res != FOUND_NOTHING);
} while (res == MADE_INFERENCE);
return FOUND_NOTHING;
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;
}
}
}
return res;
}
static update_res_t search(state_t *state)
@ -208,7 +219,7 @@ static status_t solve(int *probes_out)
res = update(&state);
if (res == FOUND_NOTHING)
res = search(&state);
} while (res != FOUND_NOTHING);
} while (res == MADE_INFERENCE);
if (check(state.field) != OK) {
printf("Incorrect inference!\n");