(Leetcode) Minesweeper game This is a Leetcode problem -


  Let's play the Minesweeper game
  (Wikipedia,
  online game)!
  
  You are given a 2D char matrix representing the game board. 'M'
  represents an unrevealed mine, 'E' represents an
  unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4
  diagonals) mines, digit (1 to 8) represents how many mines are
  adjacent to this revealed square, and finally 'X' represents a
  revealed mine.
  
  Now given the next click position (row and column indices) among all
  the unrevealed squares (M or E), return the board after
  revealing this position according to the following rules -
  
  
  If a mine (M) is revealed, then the game is over - change it to 'X'.
  If an empty square (E) with no adjacent mines is revealed, then change it to a revealed blank (B) and all of its adjacent
  unrevealed squares should be revealed recursively.
  If an empty square (E) with at least one adjacent mine is revealed, then change it to a digit (1 to 8) representing the
  number of adjacent mines.
  Return the board when no more squares will be revealed.
  
  
  Example 1 -

Input: 

[['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'M', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E']]

Click : [3,0]

Output: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'M', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

  
  Explanation -
  
  
  
  Example 2 -

Input: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'M', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Click : [1,2]

Output: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'X', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

  
  Explanation -
  
  
  
  Note -
  
  
  The range of the input matrix's height and width is [1,50].
  The click position will only be an unrevealed square (M or E), which also means the input board contains at least one clickable
  square.
  The input board won't be a stage when the game is over (some mines have been revealed).
  For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed
  mines when the game is over, consider any cases that you will win the
  game or flag any squares.
  


Here is my solution to this challenge -

class Solution:

    directions = [(-1, 0), (-1, -1), (-1, 1), (0,-1), (0, 1), (1, -1), (1, 0), (1, 1)]

    def updateBoard(self, board, click):
        """
        :type board: List[List[str]]
        :type click: List[int]
        :rtype: List[List[str]]
        """
        return self.dfs(board, click)

    def dfs(self, board, click):
        stack = [(click[0], click[1])]
        m, n = len(board), len(board[0])
        while stack:
            r, c = stack.pop() # last inserted element

            if board[r][c] == 'M':
                board[r][c] = 'X'
                break

            # check for adjacent mines
            mines = 0
            for i, j in self.directions:
                dr = r + i
                dc = c + j
                if 0 &lt;= dr &lt; m and 0 &lt;= dc &lt; n and board[dr][dc] == 'M':
                    mines += 1
            board[r][c] = str(mines) if mines else 'B'

            # add neighbors
            for i, j in self.directions:
                dr = r + i
                dc = c + j
                if 0 &lt;= dr &lt; m and 0 &lt;= dc &lt; n and board[r][c] == 'B' and board[dr][dc] == 'E':
                    stack.append((dr, dc))

        return board


Here is my Leetcode result (54 test cases) -



So, I would like to know whether I could make this program shorter and more efficient.

Any help would be highly appreciated.
An M field is never pushed. It may only happen as a result of an unfortunate click. Testing for board[r][c] == 'M' in the loop is a waste of time. Test it once, if board[click[0]][click[1]] == 'M', before the loop.
Similarly, you should not bother with neighbors if the field is not blank. A test for board[r][c] == 'B' happens too late. As implemented, for other fields code still compute neighbors - and does nothing with them.
The tests for 0 &lt;= dr &lt; m and 0 &lt;= dc &lt; n may also cause performance issues, and do not look Pythonic. Ask forgiveness not permission, e.g.

    try:
        if board[dr][dc] == 'M':
            mines += 1
    except IndexError:
        pass

Quite a few fields are pushed and inspected multiple times. I am not sure wether it is a bottleneck; however the right-hand and scanline methods of flood fill algorithm do worth investigation.