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 43 44 45 46
| class Solution { int[] dirX = {0, 1, 0, -1, 1, 1, -1, -1}; int[] dirY = {1, 0, -1, 0, 1, -1, 1, -1};
public char[][] updateBoard(char[][] board, int[] click) { int x = click[0], y = click[1]; if (board[x][y] == 'M') { board[x][y] = 'X'; } else{ dfs(board, x, y); } return board; }
public void dfs(char[][] board, int x, int y) { int cnt = 0; for (int i = 0; i < 8; ++i) { int tx = x + dirX[i]; int ty = y + dirY[i]; if (tx < 0 || tx >= board.length || ty < 0 || ty >= board[0].length) { continue; } if (board[tx][ty] == 'M') { ++cnt; } } if (cnt > 0) { board[x][y] = (char) (cnt + '0'); } else { board[x][y] = 'B'; for (int i = 0; i < 8; ++i) { int tx = x + dirX[i]; int ty = y + dirY[i]; if (tx < 0 || tx >= board.length || ty < 0 || ty >= board[0].length || board[tx][ty] != 'E') { continue; } dfs(board, tx, ty); } } } }
|