Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb343cc

Browse files
refactor 529
1 parent63d9fab commitbb343cc

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
importjava.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
publicclass_529 {
62-
publicchar[][]updateBoard(char[][]board,int[]click) {
63-
intm =board.length;
64-
intn =board[0].length;
65-
Queue<int[]>queue =newLinkedList();
66-
queue.offer(click);
67-
while (!queue.isEmpty()) {
68-
int[]curr =queue.poll();
69-
introw =curr[0];
70-
intcol =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-
intcount =0;
76-
for (inti = -1;i <2;i++) {
77-
for (intj = -1;j <2;j++) {
78-
if (i ==0 &&j ==0) {
79-
continue;
80-
}
81-
intr =row +i;
82-
intc =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+
publicstaticclassSolution1 {
68+
publicchar[][]updateBoard(char[][]board,int[]click) {
69+
intm =board.length;
70+
intn =board[0].length;
71+
Queue<int[]>queue =newLinkedList();
72+
queue.offer(click);
73+
while (!queue.isEmpty()) {
74+
int[]curr =queue.poll();
75+
intcurrRow =curr[0];
76+
intcurrCol =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+
intcount =0;
9684
for (inti = -1;i <2;i++) {
9785
for (intj = -1;j <2;j++) {
9886
if (i ==0 &&j ==0) {
9987
continue;
10088
}
101-
intr =row +i;
102-
intc =col +j;
103-
if (r >=m ||r <0 ||c >=n ||c <0) {
89+
intnextRow =currRow +i;
90+
intnextCol =currCol +j;
91+
if (nextRow >=m ||nextRow <0 ||nextCol >=n ||nextCol <0) {
10492
continue;
10593
}
106-
if (board[r][c] =='E') {
107-
queue.offer(newint[]{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 (inti = -1;i <2;i++) {
107+
for (intj = -1;j <2;j++) {
108+
if (i ==0 &&j ==0) {
109+
continue;
110+
}
111+
intnextRow =currRow +i;
112+
intnextCol =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(newint[]{nextRow,nextCol});
119+
/**then update this cell to be 'B' */
120+
board[nextRow][nextCol] ='B';
121+
}
109122
}
110123
}
111124
}
112125
}
113126
}
127+
returnboard;
114128
}
115-
returnboard;
116129
}
117-
118130
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp