6ms easy c++


  • 0
    K

    the initial state of a cell is just 'M' or 'E' where 'E' can potentially be a 'B' or a number indicating how many mines in its surrounding cells. if click on a mine just mark it 'X' and return, otherwise check the number of 'M' its surrounding has, if not 0, mark the number without dfs. if no mines in surrounding cells, continue dfs if a surrounding cell that is 'E'

    class Solution {
    public:
        vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
            dfs(board, click[0], click[1]);
            return board;
        }
        
        void dfs(vector<vector<char>>& board, int i, int j) {
            if (board[i][j] == 'M') {
                board[i][j] = 'X';
                return;
            }
            int d[][2] = {{1, 1}, {1, 0}, {1, -1}, {0, 1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
            int count = 0;
            for (auto &a : d) {
                int x = i + a[0], y = j + a[1];
                if (x >= 0 && x < board.size() && y >=0 && y < board[0].size() && board[x][y] == 'M') ++count;
            }
            if (count != 0) board[i][j] = count + '0';
            else {
                board[i][j] = 'B';
                for (auto &a : d) {
                    int x = i + a[0], y = j + a[1];
                    if (x >= 0 && x < board.size() && y >=0 && y < board[0].size() && board[x][y] == 'E')
                        dfs(board, x, y);
                }
            }
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.