Problem · Array

Process Minesweeper Game Operations

Learn this problem
MediumAmperity logoAmperityFULLTIMEPHONE SCREEN

Problem statement

Generate and play a deterministic Minesweeper game over a finite sequence of actions. The board has rows rows and cols columns, and mines lists the distinct zero-indexed mine coordinates.

Every operation has the form OPEN r c. Apply operations in order:

  • Opening a hidden mine reveals that mine and changes the game status to LOST.
  • Opening a hidden safe cell reveals its number of adjacent mines among the eight neighboring cells.
  • If that number is 0, also reveal its entire eight-directionally connected region of zero cells and every safe numbered cell bordering that region.
  • Opening an already revealed cell is a no-op.
  • The status becomes WON as soon as every non-mine cell is revealed.
  • After the game is WON or LOST, later operations leave the state unchanged.

After every operation, emit one string STATUS|BOARD. The board renders each row left to right and joins rows with /. Use # for a hidden cell, * for an opened mine, 0 for a revealed zero cell, and 1 through 8 for a revealed numbered cell. Return all emitted strings in operation order. Flag actions are not part of this exercise.

Function

playMinesweeper(rows: int, cols: int, mines: int[][], operations: String[]) → String[]

Examples

Example 1

rows = 3cols = 3mines = [[0,0]]operations = ["OPEN 2 2"]return = ["WON|#10/110/000"]

The opened zero expands through the connected zero region and reveals every bordering number. All eight safe cells become visible, so the game is won while the mine remains hidden.

Example 2

rows = 2cols = 2mines = [[0,1]]operations = ["OPEN 0 0","OPEN 0 1","OPEN 1 1"]return = ["RUNNING|1#/##","LOST|1*/##","LOST|1*/##"]

The first action reveals a numbered cell. The second opens the mine and loses the game; the last action cannot change the terminal snapshot.

Example 3

rows = 2cols = 3mines = [[0,1],[1,2]]operations = ["OPEN 0 0","OPEN 1 0","OPEN 0 2","OPEN 1 1"]return = ["RUNNING|1##/###","RUNNING|1##/1##","RUNNING|1#2/1##","WON|1#2/12#"]

These safe cells all have adjacent mines, so no flood fill occurs. The fourth action reveals the last safe cell and changes the status to WON.

Constraints

  • 1 ≤ rows, cols ≤ 50.
  • 0 ≤ mines.length < rows * cols.
  • Mine coordinates are distinct and within the board.
  • 1 ≤ operations.length ≤ 5000.
  • Every operation is a valid OPEN r c action whose coordinate lies within the board.
  • Operations are processed in their given order.

More Amperity problems

drafts saved locally
public String[] playMinesweeper(int rows, int cols, int[][] mines, String[] operations) {
  // write your code here
}
rows3
cols3
mines[[0,0]]
operations["OPEN 2 2"]
expected["WON|#10/110/000"]
checking account