Skip to content

Commit 86c5013

Browse files
update 529
1 parent 4557f41 commit 86c5013

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_529.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.Queue;
55

66
public class _529 {
7-
87
public static class Solution1 {
98
public char[][] updateBoard(char[][] board, int[] click) {
109
int m = board.length;
@@ -16,15 +15,14 @@ public char[][] updateBoard(char[][] board, int[] click) {
1615
int currRow = curr[0];
1716
int currCol = curr[1];
1817
if (board[currRow][currCol] == 'M') {
19-
/**This also covers the corner case: when click[] happens to be on a mine, then it'll exit directly.
20-
* Otherwise, we'll just continue and mark this cell to be 'M' and keep processing all 'E' cells in the queue.*/
2118
board[currRow][currCol] = 'X';
2219
} else {
23-
/**scan all four directions of this curr cell, count all mines, this includes 'X' and 'M' */
20+
/**checks all eight neighbors of this curr cell, count all mines, this includes 'X' and 'M' */
2421
int count = 0;
2522
for (int i = -1; i < 2; i++) {
2623
for (int j = -1; j < 2; j++) {
2724
if (i == 0 && j == 0) {
25+
//this is the curr cell itself, so we skip
2826
continue;
2927
}
3028
int nextRow = currRow + i;
@@ -45,7 +43,8 @@ public char[][] updateBoard(char[][] board, int[] click) {
4543
/**There is no mines around this cell, so update it to be 'B'*/
4644
board[currRow][currCol] = 'B';
4745

48-
/**then we'll also check all of its four surrounding cells, if it's 'E'. we'll also update it to be 'B' and offer it into the queue*/
46+
/**then we'll also check all of its eight surrounding cells, if it's 'E'. we'll also update it to be 'B' and offer it into the queue
47+
* Only when we know this is a 'B', we'll offer into the queue, so below check could only happen here, not in the previous nested for loop.*/
4948
for (int i = -1; i < 2; i++) {
5049
for (int j = -1; j < 2; j++) {
5150
if (i == 0 && j == 0) {

Diff for: src/test/java/com/fishercoder/_529Test.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._529;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _529Test {
10+
private static _529.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _529.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
char[][] actual = solution1.updateBoard(new char[][]{
20+
{'E', 'E', 'E', 'E', 'E'},
21+
{'E', 'E', 'M', 'E', 'E'},
22+
{'E', 'E', 'E', 'E', 'E'},
23+
{'E', 'E', 'E', 'E', 'E'},
24+
}, new int[]{3, 0});
25+
char[][] expected = new char[][]{
26+
{'B', '1', 'E', '1', 'B'},
27+
{'B', '1', 'M', '1', 'B'},
28+
{'B', '1', '1', '1', 'B'},
29+
{'B', 'B', 'B', 'B', 'B'},
30+
};
31+
assertArrayEquals(expected, actual);
32+
}
33+
}

0 commit comments

Comments
 (0)