-
Notifications
You must be signed in to change notification settings - Fork 0
/
130_SurroundedArea.txt
36 lines (36 loc) · 1.32 KB
/
130_SurroundedArea.txt
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
class Solution {
public:
void solve(vector<vector<char>>& board) {
if(board.size()==0)
return;
int m = board.size();
int n = board[0].size();
for(int i = 0;i<m;i++){
for(int j = 0;j < n;j++){
//判断是否在边界,边界上的'O'都不会被填充为'X'
bool flag = (i==0 || j==0 || i==m-1 || j==n-1);
if(flag && board[i][j]=='O' )
my_dfs(board,i,j);//找到所有与边界上的‘O’相连的‘O’
}
}
for(int i = 0;i<m;i++){
for(int j = 0;j < n;j++){
//如果不与边界'O'相连则需要被'X'填充
if(board[i][j] == 'O')
board[i][j] = 'X';
//否则还原为'O'
else if(board[i][j] == '#')
board[i][j] = 'O';
}
}
}
void my_dfs(vector<vector<char>>& board,int i,int j){
if(i >= 0 && i<board.size() && j >= 0 && j<board[0].size() && board[i][j] == 'O'){
board[i][j] = '#';//将所有与边界上的‘O’相连的‘O’改为‘#’便于之后的操作
my_dfs(board,i-1,j);//上
my_dfs(board,i+1,j);//下
my_dfs(board,i,j-1);//左
my_dfs(board,i,j+1);//右
}
}
};