-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode 130.cpp
More file actions
71 lines (63 loc) · 1.76 KB
/
Leetcode 130.cpp
File metadata and controls
71 lines (63 loc) · 1.76 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
class Solution {
private:
void dfs(int row, int col ,vector<vector<int>>&vis,vector<vector<char>>& mat, int &n, int &m ){
vis[row][col]=1;
int delrow[] = {-1,0,1,0};
int delcol[]={0,1,0,-1};
for(int i=0;i<4;i++){
int nrow = row + delrow[i];
int ncol = col + delcol[i];
if(nrow>=0 && nrow<n && ncol>=0 && ncol<m && !vis[nrow][ncol] && mat[nrow][ncol]=='O'){
dfs(nrow,ncol,vis,mat,n,m);
}
}
}
public:
void solve(vector<vector<char>>& board) {\
int n = board.size();//row
int m = board[0].size();//col
vector<vector<char>>mat = board;
vector<vector<int>>vis(n,vector<int>(m,0));
//checking the boundaries
//1st column
for(int i=0;i<n;i++){
if(mat[i][0]=='O'){
if(!vis[i][0]){
dfs(i,0,vis,mat,n,m);
}
}
}
//last column
for(int i=0;i<n;i++){
if(mat[i][m-1]=='O'){
if(!vis[i][m-1]){
dfs(i,m-1,vis,mat,n,m);
}
}
}
//1st row
for(int j=0;j<m;j++){
if(mat[0][j]=='O'){
if(!vis[0][j]){
dfs(0,j,vis,mat,n,m);
}
}
}
//last row
for(int j=0;j<m;j++){
if(mat[n-1][j]=='O'){
if(!vis[n-1][j]){
dfs(n-1,j,vis,mat,n,m);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(board[i][j]=='O' && !vis[i][j]){
mat[i][j]='X';
}
}
}
board = mat;
}
};