-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path529.minesweeper.java
More file actions
47 lines (38 loc) · 1.29 KB
/
Copy path529.minesweeper.java
File metadata and controls
47 lines (38 loc) · 1.29 KB
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
47
class Solution {
private static final int[][] DIRS = new int[][]{
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
public char[][] updateBoard(char[][] board, int[] click) {
if (board[click[0]][click[1]] == 'M') {
board[click[0]][click[1]] = 'X';
return board;
}
board[click[0]][click[1]] = 'B';
int mineCount = 0;
for (int[] dir : DIRS) {
int x = click[0] + dir[0];
int y = click[1] + dir[1];
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] == 'B') {
continue;
}
if (board[x][y] == 'M') {
mineCount++;
}
}
if (mineCount > 0) {
board[click[0]][click[1]] = (char)(mineCount + '0');
} else {
for (int[] dir : DIRS) {
int x = click[0] + dir[0];
int y = click[1] + dir[1];
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] != 'E') {
continue;
}
updateBoard(board, new int[]{x, y});
}
}
return board;
}
}