-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
35 lines (33 loc) · 968 Bytes
/
Solution.ts
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
function updateBoard(board: string[][], click: number[]): string[][] {
const m = board.length;
const n = board[0].length;
const [i, j] = click;
const dfs = (i: number, j: number) => {
let cnt = 0;
for (let x = i - 1; x <= i + 1; ++x) {
for (let 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] = cnt.toString();
return;
}
board[i][j] = 'B';
for (let x = i - 1; x <= i + 1; ++x) {
for (let y = j - 1; y <= j + 1; ++y) {
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] === 'E') {
dfs(x, y);
}
}
}
};
if (board[i][j] === 'M') {
board[i][j] = 'X';
} else {
dfs(i, j);
}
return board;
}