Difference in output when I submit vs when I run it as custom test

programming
Tags: #<Tag:0x00007f242519c210>

#1

Sorry for posting code here. But, I need some help with this.

Here is the code I am trying :
int n, m;
vector visited[100];
bool countShapes(vector &A, int i, int j){
if(i>= n || j>= m || i<0 || j<0)
return false;

if(visited[i][j]){
    return false;
}
visited[i][j] = true;

if(A[i][j] == 'O'){
    return false;   
}
else{
    countShapes(A, i, j-1);
    countShapes(A, i+1, j);
    countShapes(A, i, j+1);
    countShapes(A, i-1, j);
    return true;
}

}
int Solution::black(vector &A){
int cnt = 0;
n = A.size();
m = A[0].size();
cout<<m<<" "<<n<<endl;
//vector visited[n];
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
visited[i].push_back(false);
}
}

for(int i = 0; i<n; i++){
    for(int j = 0; j<m; j++){
        if(countShapes(A, i, j))
                   cnt++;
    }
}
return cnt;

}

Test Case : “8 XOOOOOXXOX OOXXXXOOXX XXOXXOOXXO OXOXXXXXXO XOXXOXOXXX OOOOOOOXOO XOXXXOOXOX XXXOXOXXXO” gives 6 when I submit and 7 when I run the custom test. I am puzzled with this strange behavior though I think it might have something to do with the gloabal variables that I have declared, I still don’t get the catch. Help needed.


#2

Similar problem with this code :

int n = 0, m = 0;
int cnt = 0;
void countShapes(vector &A, int i, int j, vector* visited){
if(i>= n || j>= m || i<0 || j<0)
return;

if(visited[i][j]){
    return;
}
visited[i][j] = true;
if(A[i][j] == 'O'){
    return ;   
}


countShapes(A, i, j-1, visited);
countShapes(A, i+1, j, visited);
countShapes(A, i, j+1, visited);
countShapes(A, i-1, j, visited);
return;

}
int Solution::black(vector &A){
n = A.size();
m = A[0].size();
vector visited[n];
// cout<<m<<" "<<n<<endl;
//vector visited[n];
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
visited[i].push_back(false);
}
}

for(int i = 0; i<n; i++){
    for(int j = 0; j<m; j++){
        if(!visited[i][j] && A[i][j] == 'X'){
            cnt++;    
            countShapes(A, i, j, visited);
        }
    }
}
return cnt;

}


#3

you guessed it right you should not use global variable here as the judging algorithm keeps calling the same Solution class object for different test cases in same process.


#4

That works. But, the problem was that even if I declare that way in my main, I always fill the globally declared variables with new values. Then, shouldn’t it work fine?


#5

I am having the same problem over here. When I run the test using custom case, the result is 7 as expected but when I submit the solution, the op is 8.
InterviewBit team, Could you please check if there is any error in the question or environment?