Editor

Minesweeper Clicks

#352 · Games and puzzles · Advanced · 120 XP · about 25 min

In a minesweeper game, a field of squares hides some mines. field is the hidden truth: a list of equal-length strings where '*' is a mine and '.' is safe. The player clicks squares one after another, and clicks is the list of (row, col) positions clicked, with rows and columns numbered from 0.

The player's view starts with every square closed, shown as '#'. A click works like this:

  • On a closed safe square: the square opens and shows how many of its 8 neighbours (sides and corners) hold a mine, as a digit from '0' to '8'. If that digit is '0', all its closed neighbours open too, and every newly opened square that shows '0' opens its own closed neighbours, and so on.
  • On a square that is already open: nothing happens.
  • On a mine: the game is lost. That square shows '*', and every later click is ignored.

Write open_squares(field, clicks) that returns the player's view after all the clicks, as a list of strings.

Examples

field = [
    '....*',
    '.....',
    '**...',
]
open_squares(field, [(0, 0)])
# returns ['0001#', '2211#', '#####']

open_squares(field, [(2, 4), (2, 0), (0, 0)])
# returns ['#####', '##111', '*#100']

In the first example the top left square has no mines around it, so the opening spreads until it reaches squares that touch a mine. In the second, the first click opens a small area in the bottom right corner, the click on the mine at (2, 0) ends the game, and the last click is ignored.

Constraints

  • field has 1 to 150 rows and 1 to 150 columns; every click is inside it.
  • One click can open thousands of squares, far more than Python allows for nested function calls (about 1,000), so keep your own list of squares still to open.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.