1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| class Solution { public char[][] updateBoard(char[][] board, int[] click) { int row = click[0]; int col = click[1]; if (board[row][col] == 'M') board[row][col] = 'X'; else dfs(board, row, col); return board; } private void dfs(char[][] board, int row, int col) { if (isOutOfBoundary(board, row, col) || board[row][col] != 'E') return; int mines = getNumOfMinesAround(board, row, col); if (mines == 0) { board[row][col] = 'B'; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { dfs(board, row + i, col + j); } } } else { board[row][col] = (char)('0' + mines); } } public int getNumOfMinesAround(char[][] board, int row, int col) { int result = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if ((i == j && i == 0) || isOutOfBoundary(board, row + i, col + j)) continue; if (board[row+i][col+j] == 'M') result++; } } return result; } public boolean isOutOfBoundary(char[][] board, int row, int col) { return row < 0 || row >= board.length || col < 0 || col >= board[0].length; } }
|