From 120d393f12f7cdc4327803429069350f5c2515ee Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sun, 9 Mar 2025 11:16:49 +0000 Subject: [PATCH] Fill tiles in search if same in both branches This gives a ~7% boost in solving ability. --- main.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 9930d1b..edc3d51 100644 --- a/main.c +++ b/main.c @@ -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");