Ctrl K

Open a Minefield Cell

Medium
A Minesweeper board is given as a character matrix field: - 'M': a hidden mine; - 'E': a hidden empty cell; - 'B': an opened cell with no mines around it (the neighbours are the 8 surrounding cells, diagonals included); - a digit from '1' to '8': an opened cell with that many mines around it. The player clicks the hidden cell cell = [row, col]. Return the board after this move: - if it is a mine 'M', it becomes 'X' and nothing else changes; - if it is an empty cell with mines around it, it shows their count; - if there are no mines around, the cell becomes 'B' and all its hidden empty neighbours open the same way, and so on down the chain.
Example 1
field=[['E', 'E', 'E'], ['E', 'E', 'E'], ['E', 'E', 'M']], cell=[0, 0] → [['B', 'B', 'B'], ['B', '1', '1'], ['B', '1', 'M']]

There are no mines next to (0, 0), so it becomes 'B' and the opening spreads. Cells touching the mine get '1'; the mine itself stays hidden.

Example 2
field=[['E', 'M'], ['M', 'E']], cell=[0, 0] → [['2', 'M'], ['M', 'E']]

The cell has two neighbouring mines, so it becomes '2' and nothing else opens.

Example 3
field=[['B', '1', 'E'], ['B', '1', 'M']], cell=[1, 2] → [['B', '1', 'E'], ['B', '1', 'X']]

A mine is opened: it turns into 'X' and the game is over.

Constraints
  • 1 ≤ number of rows, number of columns ≤ 50
  • Cells are 'M', 'E', 'B' or digits from '1' to '8'
  • cell points to a cell holding 'M' or 'E'
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDrain the Landlocked Ponds MediumLand Regions on a Map MediumRain Reaching Both Seas MediumCan the Ball Stop at the Target MediumShortest Path of a Rolling Ball Medium
loading Python…
Input
field=[['E', 'E', 'E'], ['E', 'E', 'E'], ['E', 'E', 'M']], cell=[0, 0]
Expected
[['B', 'B', 'B'], ['B', '1', '1'], ['B', '1', 'M']]

Run checks the first 3 tests in your browser. Submit checks all 122 tests on the server, hidden ones included.