forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
130.cpp
34 lines (27 loc) · 1.11 KB
/
130.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
class Solution {
public:
void solve(vector<vector<char>>& board) {
if(board.empty() || board[0].empty()) return;
int rows = board.size();
int cols = board[0].size();
for(int i = 0;i < rows;i++){
if(board[i][0] == 'O') dfs(board,i,0);
if(board[i][cols - 1] == 'O') dfs(board,i,cols - 1);
}
for(int j = 0;j < cols;j++){
if(board[0][j] == 'O') dfs(board,0,j);
if(board[rows - 1][j] == 'O') dfs(board,rows - 1,j);
}
for(int i = 0;i < rows;i++){
for(int j = 0;j < cols;j++){
if(board[i][j] == 'O') board[i][j] = 'X';
else if(board[i][j] == '#') board[i][j] = 'O';
}
}
}
void dfs(vector<vector<char>> &board,int row,int col){
if(row < 0 || col < 0 || row >= board.size() || col >= board[row].size() || board[row][col] != 'O') return;
board[row][col] = '#';
dfs(board,row - 1,col) , dfs(board,row + 1,col) , dfs(board,row,col - 1) , dfs(board,row,col + 1);
}
};