-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minesweeper.java
72 lines (62 loc) · 1.61 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// https://leetcode.com/problems/minesweeper/
// #dfs #bfs
class Solution {
public char[][] updateBoard(char[][] board, int[] click) {
Node root = new Node(click[0], click[1]);
Deque<Node> queue = new LinkedList<>();
queue.addLast(root);
// corner case
if (board[root.x][root.y] == 'M') {
board[root.x][root.y] = 'X';
return board;
}
// normal case
while (!queue.isEmpty()) {
Node node = queue.pollLast();
// update value at node
int mineCount = 0;
for (int i = 0; i < 8; i++) {
int x = dx[i] + node.x;
int y = dy[i] + node.y;
if (isValidSquare(board, x, y) && board[x][y] == 'M') {
mineCount++;
}
}
if (mineCount == 0) {
board[node.x][node.y] = 'B';
} else {
board[node.x][node.y] = (char) (mineCount + '0');
continue;
}
// add neighbour to the queue
for (int i = 0; i < 8; i++) {
int x = dx[i] + node.x;
int y = dy[i] + node.y;
if (isValidSquare(board, x, y) && board[x][y] == 'E') {
queue.addLast(new Node(x, y));
}
}
}
return board;
}
private static boolean isValidSquare(char[][] board, int x, int y) {
if (x < 0 || x >= board.length) {
return false;
}
if (y < 0 || y >= board[0].length) {
return false;
}
return true;
}
private static int[] dx = {-1, 0, 1, -1, 1, -1, 0, 1};
private static int[] dy = {-1, -1, -1, 0, 0, 1, 1, 1};
private static class Node {
int x;
int y;
Node(int x, int y) {
this.x = x;
this.y = y;
}
}
;
}