My code's giving segmentation error for large input, works completely fine on other IDEs, can anyone out there help?


#1

void recur(int i, int j, int c, vector &A){
A[i][j] = c+‘0’;
if(i!= A.size()-1 && A[i+1][j]==‘X’) recur(i+1,j,c,A);
if(j!=A[0].size()-1 && A[i][j+1]==‘X’) recur(i,j+1,c,A);
if(i!=0 && A[i-1][j]==‘X’) recur(i-1,j,c,A);
if(j!=0 && A[i][j-1]==‘X’) recur(i,j-1,c,A);
return;

}
int Solution::black(vector &A) {
int c=0;

for(int i=0;i<A.size();i++){
    for(int j=0;j<A[i].size();j++){
        if(A[i][j]=='X'){
            c++;
            recur(i,j,c,A);
        }
    }
}
return c;

}


#2

Please do consider the case
00000
0XXX0
0X0X0
0XXX0
00000
Please make a auxilary array to check for visited nodes as you are not taking care of cycles


#3

Just change char ‘X’ to ‘O’ ie change A[i][j]=‘O’ no need to give c as parameter in recur function


#4

int n=A[0].size()
int m=A.size();
if(i<0 || j<0)
return ;
if(i>=n || j>=m )
return;
much simpler


#5

in recur function, you are assigning A[i][j] = c+‘0’ where A[i][j] should be character only , but c+‘0’ is an integer(containing more than one digit).