Skip to content

Commit bb343cc

Browse files
refactor 529
1 parent 63d9fab commit bb343cc

File tree

1 file changed

+52
-40
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+52
-40
lines changed

src/main/java/com/fishercoder/solutions/_529.java

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Queue;
55

66
/**
7+
* 529. Minesweeper
8+
*
79
* Let's play the minesweeper game (Wikipedia, online game)!
810
911
You are given a 2D char matrix representing the game board. 'm' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.
@@ -14,6 +16,7 @@ If a mine ('m') is revealed, then the game is over - change it to 'X'.
1416
If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealed squares should be revealed recursively.
1517
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.
1618
Return the board when no more squares will be revealed.
19+
1720
Example 1:
1821
Input:
1922
@@ -33,6 +36,7 @@ If an empty square ('E') with at least one adjacent mine is revealed, then chang
3336
3437
Explanation:
3538
39+
3640
Example 2:
3741
Input:
3842
@@ -59,60 +63,68 @@ The click position will only be an unrevealed square ('m' or 'E'), which also me
5963
For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.
6064
*/
6165
public class _529 {
62-
public char[][] updateBoard(char[][] board, int[] click) {
63-
int m = board.length;
64-
int n = board[0].length;
65-
Queue<int[]> queue = new LinkedList();
66-
queue.offer(click);
67-
while (!queue.isEmpty()) {
68-
int[] curr = queue.poll();
69-
int row = curr[0];
70-
int col = curr[1];
71-
if (board[row][col] == 'M') {
72-
board[row][col] = 'X';
73-
} else {
74-
//scan the board first, find all mines, offer them into the queue
75-
int count = 0;
76-
for (int i = -1; i < 2; i++) {
77-
for (int j = -1; j < 2; j++) {
78-
if (i == 0 && j == 0) {
79-
continue;
80-
}
81-
int r = row + i;
82-
int c = col + j;
83-
if (r >= m || r < 0 || c >= n || c < 0) {
84-
continue;
85-
}
86-
if (board[r][c] == 'M' || board[r][c] == 'X') {
87-
count++;
88-
}
89-
}
90-
}
9166

92-
if (count > 0) {
93-
board[row][col] = (char) (count + '0');
67+
public static class Solution1 {
68+
public char[][] updateBoard(char[][] board, int[] click) {
69+
int m = board.length;
70+
int n = board[0].length;
71+
Queue<int[]> queue = new LinkedList();
72+
queue.offer(click);
73+
while (!queue.isEmpty()) {
74+
int[] curr = queue.poll();
75+
int currRow = curr[0];
76+
int currCol = curr[1];
77+
if (board[currRow][currCol] == 'M') {
78+
/**This also covers the corner case: when click[] happens to be on a mine, then it'll exit directly.
79+
* Otherwise, we'll just continue and mark this cell to be 'M' and keep processing all 'E' cells in the queue.*/
80+
board[currRow][currCol] = 'X';
9481
} else {
95-
board[row][col] = 'B';
82+
/**scan all four directions of this curr cell, count all mines, this includes 'X' and 'M' */
83+
int count = 0;
9684
for (int i = -1; i < 2; i++) {
9785
for (int j = -1; j < 2; j++) {
9886
if (i == 0 && j == 0) {
9987
continue;
10088
}
101-
int r = row + i;
102-
int c = col + j;
103-
if (r >= m || r < 0 || c >= n || c < 0) {
89+
int nextRow = currRow + i;
90+
int nextCol = currCol + j;
91+
if (nextRow >= m || nextRow < 0 || nextCol >= n || nextCol < 0) {
10492
continue;
10593
}
106-
if (board[r][c] == 'E') {
107-
queue.offer(new int[]{r, c});
108-
board[r][c] = 'B';
94+
if (board[nextRow][nextCol] == 'M' || board[nextRow][nextCol] == 'X') {
95+
count++;
96+
}
97+
}
98+
}
99+
100+
if (count > 0) {
101+
/**There are mines around this cell, so update it with the number of mines*/
102+
board[currRow][currCol] = (char) (count + '0');
103+
} else {
104+
/**There is no mines around this cell, so update it to be 'B'*/
105+
board[currRow][currCol] = 'B';
106+
for (int i = -1; i < 2; i++) {
107+
for (int j = -1; j < 2; j++) {
108+
if (i == 0 && j == 0) {
109+
continue;
110+
}
111+
int nextRow = currRow + i;
112+
int nextCol = currCol + j;
113+
if (nextRow >= m || nextRow < 0 || nextCol >= n || nextCol < 0) {
114+
continue;
115+
}
116+
if (board[nextRow][nextCol] == 'E') {
117+
/**we offer 'E' cells into the queue*/
118+
queue.offer(new int[]{nextRow, nextCol});
119+
/**then update this cell to be 'B' */
120+
board[nextRow][nextCol] = 'B';
121+
}
109122
}
110123
}
111124
}
112125
}
113126
}
127+
return board;
114128
}
115-
return board;
116129
}
117-
118130
}

0 commit comments

Comments
 (0)