FastPrepChoose the Next Minesweeper Action

Choose the Next Minesweeper Action

Runloop logoRunloop● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Choose one next action for a Minesweeper bot from the currently visible board. A revealed number counts mines in its at most eight neighboring cells, including diagonals. Do not include the numbered cell itself.

For this bounded practice exercise, board[r][c] is -1 for an unknown cell, -2 for an already flagged known mine, or a number from 0 through 8 for a revealed safe cell. Flags are assumed correct. Hidden mine locations are not supplied.

Explicit probability model

Assume every assignment of mine or safe to the unknown cells is initially equally likely. Retain exactly the assignments consistent with every visible number and existing flag, then treat those remaining assignments as equally likely. No global mine count is given. The input is guaranteed to admit at least one consistent assignment.

One-step action contract

  1. If no unknown cell remains, return [2, -1, -1] for DONE.
  2. If any unknown cell is a mine in every consistent assignment, return [1, row, col] for FLAG, choosing the first such cell in row-major order.
  3. Otherwise return [0, row, col] for REVEAL at the unknown cell with the lowest mine probability across all consistent assignments. Break ties by row-major order.

Coordinates are zero-based. Return only the action; do not mutate the board or simulate a reveal. The prior, board encoding, flag priority, tie rule and finite one-step scope are authored practice definitions. Minimum immediate reveal risk under this model does not claim a globally optimal strategy for winning a complete game.

Function

nextMinesweeperAction(board: int[][]) → int[]

Examples

Example 1

board = [[0,-1],[-1,-1]]return = [0,0,1]

All three unknown neighbors are safe in every consistent assignment; reveal the first in row-major order.

Example 2

board = [[1,-1]]return = [1,0,1]

The single unknown neighbor must be a mine; return FLAG at row0column1.

Example 3

board = [[-1,-1]]return = [0,0,0]

Both unknown cells have mine probability one-half under the explicit prior; reveal row0column0 by the tie rule.

Constraints

  • The rectangular board has from 1 through 4 rows and columns.
  • There are at most 15 unknown cells.
  • Every entry is -2, -1, or an integer from 0 through 8.
  • Every revealed number is consistent with at least one assignment to all unknown cells, treating existing flags as mines.
  • Use all visible clues together; assignments outside the board do not exist.

More Runloop problems

See Runloop hiring insights
public int[] nextMinesweeperAction(int[][] board) {
    // write your code here
}
board[[0,-1],[-1,-1]]
expected[0,0,1]
Checking account…