Original link: leetcode-cn.com/problems/mi…

Answer:

  1. The question only needs to find two possible situations according to one click.
  2. The first corresponds to rule 1, where the coordinates clicked are mines, i.eboard[x][y] === 'M', directly change the value of the coordinate to ‘X’, return the matrix.
  3. The second case is that the current coordinates are not mined:
    • For rule 2, count the number of mines around the current coordinate. If the number is 0, set this parameterboard[x][y] = 'B'Can. At the same time, we need to continue to recursively count the number of mines at all coordinates around it.
    • For rule 3, set to if there are mines around the current coordinatesboard[x][y] = count, there is no need to continue the recursive query.
    • If you recurse to a coordinate that is not E, you don’t need to keep looking.
/ * * *@param {character[][]} board
 * @param {number[]} click
 * @return {character[][]}* /
var updateBoard = function (board, click) {
  const [clickX, clickY] = click; // Get the coordinates of the current point

  / / rule 1
  // If you step on thunder in the first click, the game ends
  if (board[clickX][clickY] === 'M') {
    // Change the current thunder position to X
    board[clickX][clickY] = 'X';
    // Return the new matrix
    return board;
  }

  // Get the number of rows
  const xLength = board.length;
  // Get the number of columns in the matrix
  const yLength = board[0].length;
  // The row coordinates of the 8 points around the current coordinate, arranged clockwise from directly above
  const xDirection = [0.1.1.1.0, -1, -1, -1];
  // The column direction coordinates of the 8 points around the current coordinate, arranged clockwise from the top
  const yDirection = [1.1.0, -1, -1, -1.0.1];

  // Determine whether the current coordinates are outside the range of the matrix
  function isOut(x, y) {
    return x < 0 || y < 0 || x >= xLength || y >= yLength;
  }

  function dfs(x, y) {
    // If the current coordinates are outside the matrix and cannot be processed, exit the recursion
    // If the current coordinate is not E, it indicates that it has been processed
    if(isOut(x, y) || board[x][y] ! = ='E') {
      return;
    }

    // Count the number of mines around the current coordinates
    let count = 0;
    // Count the coordinates of 8 directions around the current coordinates
    let aroundPoints = [];

    for (let i = 0; i < 8; i++) {
      // Generate 8 point coordinates around the current coordinate
      let aroundX = x + xDirection[i];
      let aroundY = y + yDirection[i];

      // Only coordinates inside the matrix need to be judged
      if(! isOut(aroundX, aroundY)) {// The coordinates inside the matrix may need to be further recursively counted around the number of mines, first cached
        // If the coordinates of the surrounding points are not E, we need to join the queue for the next processing
        if (board[aroundX][aroundY] === 'E') {
          aroundPoints.push([aroundX, aroundY]);
        }

        // If there are mines around the current coordinates, count the number
        if (board[aroundX][aroundY] === 'M') { count++; }}}2 / / rules
    // If there are no mines around the current coordinates, change to B
    if (count === 0) {
      board[x][y] = 'B';

      // Continue to look around the coordinates to see if the number of mines needs to be marked
      for (let i = 0; i < aroundPoints.length; i++) {
        dfs(...aroundPoints[i]);
      }
    } else {
      / / 3
      // If the number of mines around the current coordinate is greater than 0, change it to the number of minesboard[x][y] = count.toString(); }}// From the current click position, spread out around to determine how each position is displayed
  dfs(clickX, clickY);

  // Return the new matrix
  return board;
};
Copy the code