-
Notifications
You must be signed in to change notification settings - Fork 0
/
Replace O's with X's.cpp
56 lines (51 loc) · 1.69 KB
/
Replace O's with X's.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Problem Link: https://practice.geeksforgeeks.org/problems/replace-os-with-xs0052/1
*/
class Solution{
private:
void dfs(int row, int col, vector<vector<int>>& vis, vector<vector<char>>& mat){
vis[row][col]=1;
int n= mat.size();
int m= mat[0].size();
int delrow[]={0, -1, 0, +1}; // -1,0,1,0 dont understand how same thing e.g.
int delcol[]={1, 0, -1, 0}; // 0,1,0,-1 these comment set giving wrong output
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);
}
}
}
public:
vector<vector<char>> fill(int n, int m, vector<vector<char>> mat)
{
// DFS code here
vector<vector<int>> vis(n, vector<int>(m, 0));
// traversing 1st row & last row
for(int j=0; j<m; j++){
if(!vis[0][j] && mat[0][j]=='O'){
dfs(0, j, vis, mat);
}
if(!vis[n-1][j] && mat[n-1][j]=='O'){
dfs(n-1, j, vis, mat);
}
}
// traversing 1st and last col
for(int i=0; i<n; i++){
if(!vis[i][0] && mat[i][0]=='O'){
dfs(i, 0, vis, mat);
}
if(!vis[i][m-1] && mat[i][m-1]=='O'){
dfs(i, m-1, vis, mat);
}
}
// converting remaining O to X
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(mat[i][j]=='O' && !vis[i][j]) mat[i][j]='X';
}
}
return mat;
}
};