码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode529

时间:2017-05-10 09:45:20      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:pre   upd   href   ble   ems   cells   rip   tps   cell   

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

标签:pre   upd   href   ble   ems   cells   rip   tps   cell   

原文地址:http://www.cnblogs.com/asenyang/p/6834108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!