Tcdian / keep

今天不想做,所以才去做。
MIT License
5 stars 1 forks source link

529. Minesweeper #306

Open Tcdian opened 3 years ago

Tcdian commented 3 years ago

529. Minesweeper

让我们一起来玩扫雷游戏!

给定一个代表游戏板的二维字符矩阵。 'M' 代表一个未挖出的地雷,'E' 代表一个未挖出的空方块,'B' 代表没有相邻(上,下,左,右,和所有4个对角线)地雷的已挖出的空白方块,数字('1' 到 '8')表示有多少地雷与这块已挖出的方块相邻,'X' 则表示一个已挖出的地雷。

现在给出在所有未挖出的方块中('M'或者'E')的下一个点击位置(行和列索引),根据以下规则,返回相应位置被点击后对应的面板:

  1. 如果一个地雷('M')被挖出,游戏就结束了- 把它改为 'X'。
  2. 如果一个没有相邻地雷的空方块('E')被挖出,修改它为('B'),并且所有和其相邻的未挖出方块都应该被递归地揭露。
  3. 如果一个至少与一个地雷相邻的空方块('E')被挖出,修改它为数字('1'到'8'),表示相邻地雷的数量。
  4. 如果在此次点击中,若无更多方块可被揭露,则返回面板。

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

Note

  1. 输入矩阵的宽和高的范围为 [1,50]。
  2. 点击的位置只能是未被挖出的方块 ('M' 或者 'E'),这也意味着面板至少包含一个可点击的方块。
  3. 输入面板不会是游戏结束的状态(即有地雷已被挖出)。
  4. 简单起见,未提及的规则在这个问题中可被忽略。例如,当游戏结束时你不需要挖出所有地雷,考虑所有你可能赢得游戏或标记方块的情况。
Tcdian commented 3 years ago

Solution

/**
 * @param {character[][]} board
 * @param {number[]} click
 * @return {character[][]}
 */
var updateBoard = function(board, click) {
    const [x, y] = click;
    if (board[x][y] === 'M') {
        board[x][y] = 'X';
        return board;
    }
    bfs([x, y]);
    return board;

    function bfs(start) {
        const queue = [start];
        const directionX = [-1, -1, -1, 0, 1, 1, 1, 0];
        const directionY = [-1, 0, 1, 1, 1, 0, -1, -1];
        while (queue.length !== 0) {
            const [x, y] = queue.shift();
            const nexts = [];
            let MCounts = 0;
            for (let i = 0; i < 8; i++) {
                const nextX = x + directionX[i];
                const nextY = y + directionY[i];
                if (
                    nextX >= 0 && nextX < board.length
                        && nextY >= 0 && nextY < board[0].length
                ) {
                    if (board[nextX][nextY] === 'M') {
                        MCounts += 1;
                    }
                    if (board[nextX][nextY] === 'E') {
                        nexts.push([nextX, nextY]);
                    }
                }
            }
            if (MCounts !== 0) {
                board[x][y] = String(MCounts);
            } else {
                board[x][y] = 'B';
                nexts.forEach(([nextX, nextY]) => { board[nextX][nextY] = 'W'; });
                queue.push(...nexts);
            }
        }
    }
};
function updateBoard(board: string[][], click: number[]): string[][] {
    const [x, y] = click;
    if (board[x][y] === 'M') {
        board[x][y] = 'X';
        return board;
    }
    bfs([x, y]);
    return board;

    function bfs(start: [number, number]) {
        const queue: [number, number][] = [start];
        const directionX = [-1, -1, -1, 0, 1, 1, 1, 0];
        const directionY = [-1, 0, 1, 1, 1, 0, -1, -1];
        while (queue.length !== 0) {
            const [x, y] = queue.shift() as [number, number];
            const nexts: [number, number][] = [];
            let MCounts = 0;
            for (let i = 0; i < 8; i++) {
                const nextX = x + directionX[i];
                const nextY = y + directionY[i];
                if (
                    nextX >= 0 && nextX < board.length
                        && nextY >= 0 && nextY < board[0].length
                ) {
                    if (board[nextX][nextY] === 'M') {
                        MCounts += 1;
                    }
                    if (board[nextX][nextY] === 'E') {
                        nexts.push([nextX, nextY]);
                    }
                }
            }
            if (MCounts !== 0) {
                board[x][y] = String(MCounts);
            } else {
                board[x][y] = 'B';
                nexts.forEach(([nextX, nextY]) => { board[nextX][nextY] = 'W'; });
                queue.push(...nexts);
            }
        }
    }
};