-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurrounded_Regions.cpp
More file actions
40 lines (33 loc) · 1.35 KB
/
Surrounded_Regions.cpp
File metadata and controls
40 lines (33 loc) · 1.35 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
//https://leetcode.com/problems/surrounded-regions/
void dfs(int row,int col,int n,int m, vector<vector<char>>&mat,vector<vector<int>>&vis,int dx[],int dy[]){
vis[row][col]=1;
for(int i=0;i<4;i++){
int nrow = dx[i] + row;// new row
int ncol = dy[i] + col;// new col
//check if its and O and still not visited and check if nrow and ncol are valid or not
if(nrow>=0 && nrow<n && ncol>=0 && ncol<m && !vis[nrow][ncol] && mat[nrow][ncol] == 'O')
dfs(nrow,ncol,n,m,mat,vis,dx,dy);
}
}
int n= mat.size();
int m= mat[0].size();
vector<vector<int>>vis(n,vector<int>(m,0));
//traverse rows
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
for(int i=0;i<n;i++){
for(int j = 0;j<m;j++){
if(i == 0|| i== n-1|| j == 0|| j== m-1){
if(mat[i][j] == 'O'){
dfs(i,j,n,m,mat,vis,dx,dy);
}
}
}
}
//check for 'O' which are not touched by boundary or which can be replaced
for(int i =0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j] == 'O'&& !vis[i][j]) // if it is not visited and its O
mat[i][j] = 'X'; //convert it
}
}