资讯专栏INFORMATION COLUMN

[LeetCode] 529. Minesweeper

Alex / 1404人阅读

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 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"]]

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"]]

Solution
class Solution {
    public char[][] updateBoard(char[][] board, int[] click) {
        int m = board.length, n = board[0].length;
        int row = click[0], col = click[1];
        
        if (board[row][col] == "M") {
            board[row][col] = "X";
            return board;
        }
        
        int count = 0;
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (i == 0 && j == 0) continue;
                int r = row+i, c = col+j;
                if (r < 0 || r >= m || c < 0 || c >= n) continue;
                if (board[r][c] == "M" || board[r][c] == "X") {
                    count++;
                }
            }
        }
        
        if (count > 0) board[row][col] = (char)(count+"0");
        else {
            board[row][col] = "B";
            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    if (i == 0 && j == 0) continue;
                    int r = row+i, c = col+j;
                    if (r < 0 || r >= m || c < 0 || c >= n) continue;
                    if (board[r][c] == "E") {
                        updateBoard(board, new int[]{r, c});
                    }
                }
            }
        }
        
        return board;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72233.html

相关文章

  • 容器最大盛水量

    摘要:容器最大盛水量给定个非负整数,,,,其中每个表示坐标,处的点。找到两条线,它们与轴一起形成一个容器,使得容器含有最多的水。 容器最大盛水量 Container With Most Water 给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点。 绘制n条垂直线,使得线i的两个端点在(i,ai)和(i,0)处。 找到两条线,它们与x轴一起形成一个容器,使得容器...

    luckyw 评论0 收藏0
  • 根据对象中某一属性,进行排序

    摘要:如果后台传过来的对象,顺序是被打乱的或者说,对象有多个属性,在这个页面需要按照排序,在另一个页面需要按照数量排序这里就利用字符的属性,进行排序了提供了相关的方法需要进行排序的数据一二三四值一转值成功功值七日值礼拜值调用排序方法,按照为关键字 如果后台传过来的对象,顺序是被打乱的或者说,对象有多个属性,在这个页面需要按照id排序,在另一个页面需要按照数量排序这里就利用字符的Unicode...

    QiShare 评论0 收藏0
  • 根据对象中某一属性,进行排序

    摘要:如果后台传过来的对象,顺序是被打乱的或者说,对象有多个属性,在这个页面需要按照排序,在另一个页面需要按照数量排序这里就利用字符的属性,进行排序了提供了相关的方法需要进行排序的数据一二三四值一转值成功功值七日值礼拜值调用排序方法,按照为关键字 如果后台传过来的对象,顺序是被打乱的或者说,对象有多个属性,在这个页面需要按照id排序,在另一个页面需要按照数量排序这里就利用字符的Unicode...

    VioletJack 评论0 收藏0
  • 报纸等电子报刊的客户端的实现原理解密

    摘要:已入深夜,不过忽然想起来之前倒腾过的一个关于电子报刊的客户端的时候,就写出来给大家分享一下。大功告成这篇文章主要是便于开发电子报刊应用的时候做服务端,前端,客户端三类开发人员互相了解对方需要做的事情,希望对大家有帮助,求支持。 已入深夜,不过忽然想起来之前倒腾过的一个关于电子报刊的客户端的demo时候,就写出来给大家分享一下。 我以android客户端的作为例子(IOS大家自己一样的做...

    Benedict Evans 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<