leetcode529

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode529相关的知识,希望对你有一定的参考价值。

public class Solution {
    //DFS
        public char[,] UpdateBoard(char[,] board, int[] click)
        {
            int m = board.GetLength(0), n = board.GetLength(1);
            int row = click[0], col = click[1];

            if (board[row, col] == M)
            { // Mine
                board[row, col] = X;
            }
            else
            { // Empty
                // Get number of mines first.
                int count = 0;
                for (int i = -1; i < 2; i++)
                {
                    for (int j = -1; j < 2; j++)
                    {
                        if (i == 0 && j == 0) continue;
                        int r = row + i, c = col + j;
                        if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue;
                        if (board[r, c] == M || board[r, c] == X) count++;
                    }
                }

                if (count > 0)
                { // If it is not a ‘B‘, stop further DFS.
                    board[row, col] = (char)(count + 0);
                }
                else
                { // Continue DFS to adjacent cells.
                    board[row, col] = B;
                    for (int i = -1; i < 2; i++)
                    {
                        for (int j = -1; j < 2; j++)
                        {
                            if (i == 0 && j == 0) continue;
                            int r = row + i, c = col + j;
                            if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue;
                            if (board[r, c] == E) UpdateBoard(board, new int[] { r, c });
                        }
                    }
                }
            }
            return board;
        }

        ////BFS
        //public char[,] UpdateBoard(char[,] board, int[] click)
        //{
        //    int m = board.GetLength(0), n = board.GetLength(1);
        //    Queue<int[]> queue = new Queue<int[]>();
        //    queue.Enqueue(click);

        //    while (queue.Count > 0)
        //    {
        //        int[] cell = queue.Dequeue();
        //        int row = cell[0], col = cell[1];

        //        if (board[row, col] == ‘M‘)
        //        { // Mine
        //            board[row, col] = ‘X‘;
        //        }
        //        else
        //        { // Empty
        //            // Get number of mines first.
        //            int count = 0;
        //            for (int i = -1; i < 2; i++)
        //            {
        //                for (int j = -1; j < 2; j++)
        //                {
        //                    if (i == 0 && j == 0) continue;
        //                    int r = row + i, c = col + j;
        //                    if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue;
        //                    if (board[r, c] == ‘M‘ || board[r, c] == ‘X‘) count++;
        //                }
        //            }

        //            if (count > 0)
        //            { // If it is not a ‘B‘, stop further DFS.
        //                board[row, col] = (char)(count + ‘0‘);
        //            }
        //            else
        //            { // Continue BFS to adjacent cells.
        //                board[row, col] = ‘B‘;
        //                for (int i = -1; i < 2; i++)
        //                {
        //                    for (int j = -1; j < 2; j++)
        //                    {
        //                        if (i == 0 && j == 0) continue;
        //                        int r = row + i, c = col + j;
        //                        if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue;
        //                        if (board[r, c] == ‘E‘)
        //                        {
        //                            queue.Enqueue(new int[] { r, c });
        //                            board[r, c] = ‘B‘; // Avoid to be added again.
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    return board;
        //}
}

https://leetcode.com/problems/minesweeper/#/description

以上是关于leetcode529的主要内容,如果未能解决你的问题,请参考以下文章

leetcode529

Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper)

[LeetCode] 529. Minesweeper_ Medium_ tag: BFS

529. Minesweeper

# Codeforces Round #529(Div.3)个人题解

LeetCode 0529. 扫雷游戏