C++ Dfs Traversing only boundaries


#1
 `void Dfs(vector<vector<char> >&A,int i,int j,int n,int m){
if(i >= n || i < 0 || j >= m || j < 0) return;
if(A[i][j] == 'X' || A[i][j] == '.') return;

A[i][j]='.';

Dfs(A,i+1,j,n,m);
Dfs(A,i-1,j,n,m);
Dfs(A,i,j+1,n,m);
Dfs(A,i,j-1,n,m);

}void Solution::solve(vector<vector > &A) {
if(A.size() == 0) return;
int n = A.size(),m = A[0].size();
for(int i=0;i<n;i++){
if(A[i][0] == ‘O’){
Dfs(A,i,0,n,m);
}
}
for(int i=0;i<n;i++){
if(A[i][m-1] == ‘O’){
Dfs(A,i,m-1,n,m);
}
}
for(int j=0;j<m;j++){
if(A[0][j] == ‘O’){
Dfs(A,0,j,n,m);
}
}
for(int j=0;j<m;j++){
if(A[n-1][j] == ‘O’){
Dfs(A,n-1,j,n,m);
}
}

for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        if(A[i][j] == '.') A[i][j] = 'O';
        else if(A[i][j] == 'O') A[i][j] = 'X';
    }
}

}`