-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurroundedRegions.cpp
More file actions
77 lines (61 loc) · 2.12 KB
/
SurroundedRegions.cpp
File metadata and controls
77 lines (61 loc) · 2.12 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Solution {
private:
int rows;
int cols;
public:
void solve(vector<vector<char>>& board) {
rows = board.size();
if(!rows) return;
cols = board[0].size();
if(rows<3 || cols<3)
return;
for(int col =0; col<cols; ++col){
if(board[0][col]=='O')
BFSBOARD(board,0,col);
if(board[rows-1][col]=='O')
BFSBOARD(board,rows-1, col);
}
for(int row =0; row<rows; ++row){
if(board[row][0]=='O')
BFSBOARD(board,row,0);
if(board[row][cols-1]=='O')
BFSBOARD(board,row, cols-1);
}
for(int i =0 ; i<rows; i++){
for(int j =0; j<cols; j++){
if(board[i][j]!='X'){
board[i][j] = board[i][j]=='F'?'O':'X';
}
}
}
return;
}
void BFSBOARD(vector<vector<char>>& board,int i, int j){
board[i][j]='F';
queue<pair<int,int>> myQueue;
myQueue.push(make_pair(i,j));
while(!myQueue.empty()){
int row = myQueue.front().first;
int col = myQueue.front().second;
myQueue.pop();
//Now lets take four conditions of surroundings
if(row>1 && board[row-1][col]=='O'){
board[row-1][col]='F';
myQueue.push(make_pair(row-1, col));
}
if(row<rows-2 && board[row+1][col]=='O'){
board[row+1][col]='F';
myQueue.push(make_pair(row+1, col));
}
if(col>1 && board[row][col-1]=='O'){
board[row][col-1]='F';
myQueue.push(make_pair(row, col-1));
}
if(col<cols-2 && board[row][col+1]=='O'){
board[row][col+1]='F';
myQueue.push(make_pair(row, col+1));
}
}
return;
}
};