-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMinesweeper.java
320 lines (287 loc) · 10.5 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package LeetCodeJava.DFS;
// https://leetcode.com/problems/minesweeper/description/
// https://leetcode.cn/problems/minesweeper/description/
import java.util.LinkedList;
import java.util.Queue;
/**
* 529. Minesweeper
* Solved
* Medium
* Topics
* Companies
* Let's play the minesweeper game (Wikipedia, online game)!
*
* You are given an m x n char matrix board representing the game board where:
*
* 'M' represents an unrevealed mine,
* 'E' represents an unrevealed empty square,
* 'B' represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),
* digit ('1' to '8') represents how many mines are adjacent to this revealed square, and
* 'X' represents a revealed mine.
* You are also given an integer array click where click = [clickr, clickc] represents the next click position among all the unrevealed squares ('M' or 'E').
*
* Return the board after revealing this position according to the following rules:
*
* If a mine 'M' is revealed, then the game is over. You should change it to 'X'.
* If an empty square 'E' with no adjacent mines is revealed, then change it to a revealed blank 'B' and all of its adjacent unrevealed squares should be revealed recursively.
* If an empty square 'E' with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
* Return the board when no more squares will be revealed.
*
*
* Example 1:
*
*
* Input: board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0]
* Output: [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
* Example 2:
*
*
* Input: board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
* Output: [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
*
*
* Constraints:
*
* m == board.length
* n == board[i].length
* 1 <= m, n <= 50
* board[i][j] is either 'M', 'E', 'B', or a digit from '1' to '8'.
* click.length == 2
* 0 <= clickr < m
* 0 <= clickc < n
* board[clickr][clickc] is either 'M' or 'E'.
*
*
*/
public class Minesweeper {
// V0
// public char[][] updateBoard(char[][] board, int[] click) {
//
// }
// V1
// IDEA: DFS + ARRAY OP (GPT)
public char[][] updateBoard_1(char[][] board, int[] click) {
int rows = board.length;
int cols = board[0].length;
int x = click[0], y = click[1];
// Edge case: 1x1 grid
if (rows == 1 && cols == 1) {
if (board[0][0] == 'M') {
board[0][0] = 'X';
} else {
board[0][0] = 'B'; // Fix: properly set 'B' if it's 'E'
}
return board;
}
// If a mine is clicked, mark as 'X'
if (board[x][y] == 'M') {
board[x][y] = 'X';
return board;
}
// Otherwise, reveal cells recursively
reveal_1(board, x, y);
return board;
}
private void reveal_1(char[][] board, int x, int y) {
int rows = board.length;
int cols = board[0].length;
// Boundary check and already revealed check
/** NOTE !!!
*
* - 1) 'E' represents an unrevealed empty square,
*
* - 2) board[x][y] != 'E'
* -> ensures that we only process unrevealed empty cells ('E')
* and avoid unnecessary recursion.
*
* - 3) board[x][y] != 'E'
* • Avoids re-processing non-‘E’ cells
* • The board can have:
* • 'M' → Mine (already handled separately)
* • 'X' → Clicked mine (game over case)
* • 'B' → Blank (already processed)
* • '1' to '8' → Number (already processed)
* • If a cell is not 'E', it means:
* • It has already been processed
* • It does not need further expansion
* • This prevents infinite loops and redundant checks.
*
*
* - 4) example:
*
* input:
* E E E
* E M E
* E E E
*
* Click at (0,0)
* 1. We call reveal(board, 0, 0), which:
* • Counts 1 mine nearby → Updates board[0][0] = '1'
* • Does NOT recurse further, avoiding unnecessary work.
*
* What If We Didn’t Check board[x][y] != 'E'?
* • It might try to expand into already processed cells, leading to redundant computations or infinite recursion.
*
*/
if (x < 0 || x >= rows || y < 0 || y >= cols || board[x][y] != 'E') {
return;
}
// Directions for 8 neighbors
int[][] directions = {
{ -1, -1 }, { -1, 0 }, { -1, 1 },
{ 0, -1 }, { 0, 1 },
{ 1, -1 }, { 1, 0 }, { 1, 1 }
};
// Count adjacent mines
int mineCount = 0;
for (int[] dir : directions) {
int newX = x + dir[0];
int newY = y + dir[1];
if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && board[newX][newY] == 'M') {
mineCount++;
}
}
// If there are adjacent mines, show count
if (mineCount > 0) {
board[x][y] = (char) ('0' + mineCount);
} else {
// Otherwise, reveal this cell and recurse on neighbors
board[x][y] = 'B';
for (int[] dir : directions) {
reveal_1(board, x + dir[0], y + dir[1]);
}
}
}
// V2
// IDEA: DFS
// https://leetcode.com/problems/minesweeper/solutions/495383/java-simple-recursive-solution-with-comm-5mtf/
public char[][] updateBoard_2(char[][] board, int[] click) {
// once a mine is revealed, we can terminate immediately
if (board[click[0]][click[1]] == 'M') {
board[click[0]][click[1]] = 'X';
return board;
}
reveal(board, click[0], click[1]);
return board;
}
private void reveal(char[][] board, int i, int j) {
// edge cases
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != 'E')
return;
board[i][j] = '0';
int[][] neighbors = { { i - 1, j - 1 }, { i - 1, j }, { i - 1, j + 1 },
{ i, j - 1 }, { i, j + 1 },
{ i + 1, j - 1 }, { i + 1, j }, { i + 1, j + 1 } };
// check all neighbors for number of mines
for (int[] neighbor : neighbors) {
if (neighbor[0] < 0 || neighbor[1] < 0 || neighbor[0] >= board.length || neighbor[1] >= board[0].length)
continue;
if (board[neighbor[0]][neighbor[1]] == 'M')
board[i][j]++;
}
if (board[i][j] != '0')
return;
// all neighbors are empty, recursively reveal them
board[i][j] = 'B';
for (int[] neighbor : neighbors)
reveal(board, neighbor[0], neighbor[1]);
}
// V3-1
// IDEA: BFS
// https://leetcode.com/problems/minesweeper/solutions/1524772/java-tc-omn-sc-omn-bfs-dfs-solutions-by-n589j/
private static final int[][] DIRS = { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 }, { -1, 0 },
{ -1, 1 } };
public char[][] updateBoard_3_1(char[][] board, int[] click) {
if (board == null || click == null) {
throw new IllegalArgumentException("Inputs are null");
}
if (board[click[0]][click[1]] != 'M' && board[click[0]][click[1]] != 'E') {
return board;
}
if (board[click[0]][click[1]] == 'M') {
board[click[0]][click[1]] = 'X';
return board;
}
int mines = getMinesCount(board, click[0], click[1]);
if (mines != 0) {
board[click[0]][click[1]] = (char) (mines + '0');
return board;
}
board[click[0]][click[1]] = 'B';
Queue<int[]> queue = new LinkedList<>();
queue.offer(click);
while (!queue.isEmpty()) {
int[] cur = queue.poll();
for (int[] d : DIRS) {
int x = cur[0] + d[0];
int y = cur[1] + d[1];
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] != 'E') {
continue;
}
mines = getMinesCount(board, x, y);
if (mines != 0) {
board[x][y] = (char) (mines + '0');
continue;
}
board[x][y] = 'B';
queue.offer(new int[] { x, y });
}
}
return board;
}
private int getMinesCount(char[][] board, int x, int y) {
int mines = 0;
for (int[] d : DIRS) {
int r = x + d[0];
int c = y + d[1];
if (r >= 0 && r < board.length && c >= 0 && c < board[0].length && board[r][c] == 'M') {
mines++;
}
}
return mines;
}
// 3-2
// IDEA: DFS
// https://leetcode.com/problems/minesweeper/solutions/1524772/java-tc-omn-sc-omn-bfs-dfs-solutions-by-n589j/
// private static final int[][] DIRS = { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 }, { -1, 0 },
// { -1, 1 } };
public char[][] updateBoard(char[][] board, int[] click) {
if (board == null || click == null) {
throw new IllegalArgumentException("Inputs are null");
}
if (board[click[0]][click[1]] != 'M' && board[click[0]][click[1]] != 'E') {
return board;
}
if (board[click[0]][click[1]] == 'M') {
board[click[0]][click[1]] = 'X';
return board;
}
revealBoard(board, click[0], click[1]);
return board;
}
private void revealBoard(char[][] board, int x, int y) {
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] != 'E') {
return;
}
int mines = getMinesCount_2(board, x, y);
if (mines != 0) {
board[x][y] = (char) (mines + '0');
return;
}
board[x][y] = 'B';
for (int[] d : DIRS) {
revealBoard(board, x + d[0], y + d[1]);
}
}
private int getMinesCount_2(char[][] board, int x, int y) {
int mines = 0;
for (int[] d : DIRS) {
int r = x + d[0];
int c = y + d[1];
if (r >= 0 && r < board.length && c >= 0 && c < board[0].length && board[r][c] == 'M') {
mines++;
}
}
return mines;
}
}