Process Minesweeper Game Operations
Updated
Problem statement
Play a Minesweeper game by applying a list 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
WONas soon as every non-mine cell is revealed. - After the game is
WONorLOST, 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 board because the game is already over.
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 caction whose coordinate lies within the board. - Operations are processed in their given order.
More Amperity Phone Screen questions
Similar questions at other companies
- Drop and Eliminate Adjacent PiecesSnowflake · PHONE SCREENSnowflake interview questions
- Minesweeper Board UpdateUber · ONSITE INTERVIEWUber interview questions
- Minesweeper Board UpdateClay · ONSITE INTERVIEW
- Minesweeper Board UpdateAirbyte · ONSITE INTERVIEW
- Matrix EscapeByteDance · PHONE SCREENByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Grid Infection with Recovery After D DaysOpenAI · PHONE SCREENOpenAI interview questions
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
- Days Until Poisoned Plants StabilizeApple · PHONE SCREENApple interview questions