Skip to content

Commit 2e1959a

Browse files
authored
Create SurroundedRegions.cpp
1 parent 9fdc452 commit 2e1959a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

SurroundedRegions.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
void fillcolor(vector<vector<char>>&board,int x,int y){
4+
if(x<0||y<0||x>=board.size()||y>=board[0].size()){
5+
return;
6+
}
7+
if(board[x][y]=='X'||board[x][y]=='C'){
8+
return;
9+
}
10+
if(board[x][y]=='O'){
11+
board[x][y]='C';
12+
}
13+
fillcolor(board,x-1,y);
14+
fillcolor(board,x,y-1);
15+
fillcolor(board,x+1,y);
16+
fillcolor(board,x,y+1);
17+
18+
}
19+
void solve(vector<vector<char>>& board) {
20+
for(int i=0;i<board.size();i++){
21+
for(int j=0;j<board[0].size();j++){
22+
if(i==0||j==0||i==board.size()-1||j==board[0].size()-1){
23+
fillcolor(board,i,j);
24+
}
25+
}
26+
}
27+
for(int i=0;i<board.size();i++){
28+
for(int j=0;j<board[0].size();j++){
29+
if(board[i][j]=='C'){
30+
board[i][j]='O';
31+
}
32+
else{
33+
board[i][j]='X';
34+
}
35+
}
36+
}
37+
38+
}
39+
};

0 commit comments

Comments
 (0)