forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.cpp
39 lines (37 loc) · 1.21 KB
/
Minesweeper.cpp
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
// Runtime: 56 ms (Top 19.17%) | Memory: 11.7 MB (Top 93.43%)
class Solution {
public:
int m, n ;
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
if(board[click[0]][click[1]] == 'M'){
board[click[0]][click[1]] = 'X' ;
return board ;
}
else{
m = board.size(), n = board[0].size() ;
dfs(click[0], click[1], board) ;
}
return board ;
}
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, -1, 0, 1, 1, -1, -1, 1};
void dfs(int cr, int cc, vector<vector<char>> &board){
int count = 0 ;
for(int i = 0 ; i < 8 ; i++){
int nr = cr + dx[i], nc = cc + dy[i] ;
if(nr<0 || nr>=m || nc<0 || nc >=n || board[nr][nc]!='M') continue;
count++ ;
}
if(count!=0){
board[cr][cc] = '0'+count ;
return ;
}else{
board[cr][cc] = 'B' ;
for(int i = 0 ; i < 8 ; i++){
int nr = cr + dx[i], nc = cc + dy[i] ;
if(nr<0 || nr>=m || nc<0 || nc >=n || board[nr][nc]!='E') continue;
dfs(nr, nc, board) ;
}
}
}
};