Minesweeper - Complete Solution Guide
Minesweeper is LeetCode problem 529, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Let's play the minesweeper game ( Wikipedia , online game )! You are given an m x n char matrix board representing the game board where: 'M' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals), digit ( '1' to '8' ) represents how many mines are adjacent to this revealed square, and 'X' represents a revealed mine. You are also given an integer array click
Detailed Explanation
The Minesweeper problem asks us to simulate a simplified version of the Minesweeper game. We're given a 2D board represented as a character matrix and the coordinates of a click on the board. The board contains unrevealed mines ('M'), unrevealed empty squares ('E'), revealed blank squares ('B'), revealed squares with adjacent mine counts ('1' to '8'), and revealed mines ('X'). We need to update the board based on where the player clicks according to these rules: 1. If the click reveals a mine ('M'), change it to 'X' (game over). 2. If the click reveals an empty square ('E') with no adjacent mines, change it to 'B' and recursively reveal all adjacent unrevealed squares. 3. If the click reveals an empty square ('E') with at least one adjacent mine, change it to a digit representing the number of adjacent mines. 4. If neither of the above, return the board.
Solution Approach
The provided code uses Breadth-First Search (BFS) to solve the Minesweeper problem. The BFS approach starts at the clicked cell and explores its neighbors in a layer-by-layer fashion. It correctly simulates the Minesweeper game logic by checking for mines upon clicking and recursively revealing adjacent cells if an empty cell without adjacent mines is revealed. A `visited` set prevents revisiting cells, ensuring the algorithm terminates correctly.
Step-by-Step Algorithm
- Step 1: Check if the clicked cell is a mine ('M'). If so, change it to 'X' and return the board.
- Step 2: Initialize a queue and a visited set. Add the clicked cell to both.
- Step 3: While the queue is not empty, dequeue a cell.
- Step 4: For the dequeued cell, count the number of adjacent mines.
- Step 5: If the mine count is greater than 0, update the cell with the mine count (converted to a char).
- Step 6: If the mine count is 0, update the cell to 'B' and enqueue all unvisited adjacent empty ('E') cells, adding them to the visited set.
- Step 7: Repeat steps 3-6 until the queue is empty.
- Step 8: Return the updated board.
Key Insights
- Insight 1: Use Breadth-First Search (BFS) or Depth-First Search (DFS) to recursively reveal adjacent cells when clicking on an empty cell with no adjacent mines.
- Insight 2: Before revealing any cell, count the number of adjacent mines to determine the value to display or whether to trigger the recursive revealing process.
- Insight 3: Use a `visited` set/matrix to avoid infinite loops during the recursive reveal process by ensuring each cell is visited only once.
Complexity Analysis
Time Complexity: O(M*N)
Space Complexity: O(M*N)
Topics
This problem involves: Array, Depth-First Search, Breadth-First Search, Matrix.
Companies
Asked at: Anduril, Applied Intuition, Robinhood, Yext.