-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.java
44 lines (37 loc) · 1.59 KB
/
Minesweeper.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
42
43
44
class Solution {
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
public char[][] updateBoard(char[][] board, int[] click) {
Queue<int[]> q = new LinkedList<>();
int m = board.length, n = board[0].length;
boolean[][] visited = new boolean[m][n];
// If click on M, game over
if(board[click[0]][click[1]] == 'M') {
board[click[0]][click[1]] = 'X';
return board;
}
// Game continues
q.offer(click);
while(!q.isEmpty()) {
Set<int[]> temp = new HashSet<>();
int[] pos = q.poll();
visited[pos[0]][pos[1]] = true;
// If E, check adjacent
if(board[pos[0]][pos[1]] == 'E') {
int count = 0;
for(int[] dir: dirs) {
int row = pos[0] + dir[0], col = pos[1] + dir[1];
if(row < 0 || col < 0 || row >= m || col >= n || visited[row][col] || board[row][col] == 'B') continue;
if(board[row][col] == 'M') count++;
else if(board[row][col] == 'E') temp.add(new int[] {row, col});
}
// Only add to queue if current pos is B
if(count == 0) {
board[pos[0]][pos[1]] = 'B';
q.addAll(temp);
}
else board[pos[0]][pos[1]] = (char)('0' + count);
}
}
return board;
}
}