Java solution, passing coordinate position as int not int[]


  • 0
    B

    DFS

    public class Solution {
        public char[][] updateBoard(char[][] board, int[] click) {
            if (board[click[0]][click[1]] == 'M') {
                board[click[0]][click[1]] = 'X';
                return board;
            }
            
            updateBoard(board, click[0] * 50 + click[1]);
            return board;
        }
        private void updateBoard(char[][] board, int click) {
            int row = click / 50;
            int col = click % 50;
            int count = 0;
            for (int i = Math.max(0, row - 1); i <= Math.min(row + 1, board.length - 1); i++) {
                for (int j = Math.max(0, col - 1); j <= Math.min(col + 1, board[0].length - 1); j++) {
                    if (board[i][j] == 'M') {
                        count++;
                    }
                }
            }
            
            if (count > 0) {
                board[row][col] = (char)(count + '0');
            } else {
                board[row][col] = 'B';
                for (int i = Math.max(0, row - 1); i <= Math.min(row + 1, board.length - 1); i++) {
                    for (int j = Math.max(0, col - 1); j <= Math.min(col + 1, board[0].length - 1); j++) {
                        if (board[i][j] == 'E') {
                            updateBoard(board, i * 50 + j);
                        }
                    }
                }
            }        
        }
    }
    

    BFS

    public class Solution {
        public char[][] updateBoard(char[][] board, int[] click) {
            Deque<Integer> queue = new LinkedList<>();
            queue.addLast(click[0] * 50 + click[1]);
            while (!queue.isEmpty()) {
                int cur = queue.pollFirst();
                int row = cur / 50;
                int col = cur % 50;
                
                if (board[row][col] == 'M') {
                    board[row][col] = 'X';
                    return board;
                }
                
                // find out if there is any mine surround cur
                int count = 0;
                for (int i = Math.max(0, row - 1); i <= Math.min(row + 1, board.length - 1); i++) {
                    for (int j = Math.max(0, col - 1); j <= Math.min(col + 1, board[0].length - 1); j++) {
                        if (board[i][j] == 'M') {
                            count++;
                        }
                    }
                }
        
                if (count > 0) {
                    board[row][col] = (char)(count + '0');
                } else {
                    board[row][col] = 'B';
                    for (int i = Math.max(0, row - 1); i <= Math.min(row + 1, board.length - 1); i++) {
                        for (int j = Math.max(0, col - 1); j <= Math.min(col + 1, board[0].length - 1); j++) {
                            if (board[i][j] == 'E') {
                                queue.addLast(i * 50 + j);
                                board[i][j] = 'U';
                            }
                        }
                    }
                }
            }
            return board;
        }
    }
    

Log in to reply
 

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