Easy DFS Java Solution


  • 0
    public class Solution {
        public char[][] updateBoard(char[][] board, int[] click) {
            int x = click[0], y = click[1];
            if(board[x][y] == 'M') 
            {
                board[x][y] = 'X';
            }
            if(board[x][y] == 'E')
            {
                helper(board, x, y);
            }
            return board;
        }
        public void helper(char[][]board, int x, int y)
        {
            if(board[x][y] != 'E') return;
            board[x][y] = 'B';
            int mine = 0;
            int[] row = {-1, 0, 0, 1, -1, -1, 1, 1};
            int[] col = {0, -1, 1, 0, -1, 1, -1, 1};
            for(int i = 0; i < 8; i++)
            {
                int r = x + row[i];
                int c = y + col[i];
                if(r >= 0 && r < board.length && c >= 0 && c < board[0].length)
                {
                    if(board[r][c] == 'M')
                    {
                        mine++;
                        board[x][y] = (char)(mine + '0');
                    }
                }
            }
            if(mine == 0)
            {
                for(int i = 0; i < 8; i++)
                {
                    int r = x + row[i];
                    int c = y + col[i];
                    if(r >= 0 && r < board.length && c >= 0 && c < board[0].length)
                    {
                        helper(board, r, c);
                    }
                }
            }
        }
    }
    
    
    

Log in to reply
 

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