Getting WA when I am using visiting Array


#1

#define pb push_back
#define mp make_pair
#define fi first
#define se second
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};
map<pair<int,int>,int> vis;
int n,m;
int issafe(int x,int y,int n,int m)
{
if(x<0 || x>=n || y<0 || y>=m)
return 0;
return 1;
}
void dfs(pair<int,int> p,vector<vector> &A)
{
A[p.fi][p.se]=’.’;
vis[p]=1;
for(int i=0;i<4;i++)
{
int x1 = p.fi+dx[i];
int y1 = p.se+dy[i];
if(issafe(x1,y1,n,m) && vis[{x1,y1}]==0 && A[x1][y1]==‘O’)
dfs(mp(x1,y1),A);
}
}
void Solution::solve(vector<vector > &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
n=A.size();
m = A[0].size();
// queue<pair<int,int>> q;
// q.push(mp(0,0));
for(int i=0;i<n;i++){
if(A[i][0] == ‘O’){
dfs(mp(i,0),A);
}
}
for(int i=0;i<n;i++){
if(A[i][m-1] == ‘O’){
dfs(mp(i,m-1),A);
}
}
for(int j=0;j<m;j++){
if(A[0][j] == ‘O’){
dfs(mp(0,j),A);
}
}
for(int j=0;j<m;j++){
if(A[n-1][j] == ‘O’){
dfs(mp(n-1,j),A);
}
}
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’;
}
}