-
Notifications
You must be signed in to change notification settings - Fork 5
/
Minesweeper.java
43 lines (40 loc) · 1.38 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
/*https://leetcode.com/problems/minesweeper/*/
class Solution {
boolean[][] visited;
int m, n;
int[][] pos = new int[][]{{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
public char[][] updateBoard(char[][] board, int[] click) {
if (board[click[0]][click[1]] == 'M')
{
board[click[0]][click[1]] = 'X';
return board;
}
m = board.length; n = board[0].length;
visited = new boolean[m][n];
board = reveal(board,click[0],click[1]);
return board;
}
public char[][] reveal(char[][] board, int row, int col)
{
if (row < 0 || row >= m || col < 0 || col >= n || visited[row][col]) return board;
visited[row][col] = true;
int result = 0, i, temp;
for (i = 0; i < 8; ++i)
result += checkAdjacentCells(board, row+pos[i][0], col+pos[i][1]);
if (result == 0)
{
board[row][col] = 'B';
for (i = 0; i < 8; ++i)
board = reveal(board, row+pos[i][0], col+pos[i][1]);
return board;
}
board[row][col] = Integer.toString(result).charAt(0);
return board;
}
public int checkAdjacentCells(char[][] board, int row, int col)
{
if (row < 0 || row >= m || col < 0 || col >= n) return 0;
if (board[row][col] == 'M') return 1;
return 0;
}
}