-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
41 lines (39 loc) · 1.11 KB
/
Solution.java
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
class Solution {
private char[][] board;
private int m;
private int n;
public char[][] updateBoard(char[][] board, int[] click) {
m = board.length;
n = board[0].length;
this.board = board;
int i = click[0], j = click[1];
if (board[i][j] == 'M') {
board[i][j] = 'X';
} else {
dfs(i, j);
}
return board;
}
private void dfs(int i, int j) {
int cnt = 0;
for (int x = i - 1; x <= i + 1; ++x) {
for (int y = j - 1; y <= j + 1; ++y) {
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M') {
++cnt;
}
}
}
if (cnt > 0) {
board[i][j] = (char) (cnt + '0');
} else {
board[i][j] = 'B';
for (int x = i - 1; x <= i + 1; ++x) {
for (int y = j - 1; y <= j + 1; ++y) {
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E') {
dfs(x, y);
}
}
}
}
}
}