forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateBoard.java
More file actions
51 lines (46 loc) · 1.81 KB
/
UpdateBoard.java
File metadata and controls
51 lines (46 loc) · 1.81 KB
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
package serach;
import java.util.Arrays;
public class UpdateBoard {
public char[][] updateBoard(char[][] board,int[] click){
int m = board.length,n = board[0].length;
int row = click[0],col = click[1];
if (board[row][col] == 'M'){// Mine
board[row][col] = 'X';
}else {// Empty
// Get number of mines first.
int count = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) continue;
int r = row + i,c= col + j;
if(r<0 || r>=m || c<0 || c<0 || c>=n) continue;
if (board[r][c] == 'M' || board[r][c] == 'X') count++;
}
}
if (count > 0){// If it is not a 'B',stop further DFS
board[row][col] = (char) (count + '0');
}else {// Continue DFS to adjacent cells
board[row][col] = 'B';
for (int i = -1;i<2;i++){
for (int j = -1; j <2 ; j++) {
if (i==0 && j==0) continue;
int r = row + i,c = col + j;
if (r<0 || r>=m || c<0 || c>=n) continue;
if (board[r][c] == 'E') updateBoard(board,new int[]{r,c});
}
}
}
}
return board;
}
public static void main(String[] args) {
char[][] board = {{'E', 'E', 'E', 'E', 'E'},
{'E', 'E', 'M', 'E', 'E'},
{'E', 'E', 'E', 'E', 'E'},
{'E', 'E', 'E', 'E', 'E'}};
int[] click = {3,0};
UpdateBoard updateBoard = new UpdateBoard();
char[][] r = updateBoard.updateBoard(board,click);
System.out.println(Arrays.asList(Arrays.asList(r)));
}
}