There was a problem In Encoders 2020 Contest ->> Contaminated Matrix

Though the problem was easy but not getting AC particularly from this code. Spend a lot debugging it but not able to find the mistake or the Target case for which my code is failing…
If anyone can help please do…
#include<bits/stdc++.h>

using namespace std;

#define int long long

#define me 1000000007

#define pd push_back

#define f(a,b,c) for(int i=a;i<b;i+=c)

int solve(){

int n,m; cin>>n>>m;

int mat[n][m];

for(int i=0;i<n;i++)

{

    for(int j=0;j<m;j++)

    cin>>mat[i][j];

}

for(int i=0;i<n;i++)

{

    for(int j=0;j<m;j++)

    {

       if(mat[i][j] == 1)

       {

           if((i+1)<n && mat[i+1][j]==0)  // col

           mat[i+1][j] = 3;

           if((j+1)<m && mat[i][j+1]==0)  // row

           mat[i][j+1] = 2; 

       } 

       else if(mat[i][j] == 2)

       {

           if((j+1)<m && mat[i][j+1]==0) // row 

           mat[i][j+1] = 2;

       }

       else if(mat[i][j] == 3)

       {

           if((i+1)<n && mat[i+1][j]==0) //col

           mat[i+1][j] = 3;

       }

    }

}

for(int i=n-1;i>=0;i--)

{

    for(int j=m-1;j>=0;j--)

    {

       if(mat[i][j] == 1)

       {

           if((i-1)>=0 && mat[i-1][j]==0)

           mat[i-1][j] = 3;

           if((j-1)>=0 && mat[i][j-1]==0)

           mat[i][j-1] = 2; 

       } 

       else if(mat[i][j] == 2)

       {

           if((j-1)>=0 && mat[i][j-1]==0)

           mat[i][j-1] = 2;

       }

       else if(mat[i][j] == 3)

       {

           if((i-1)>=0 && mat[i-1][j]==0)

           mat[i-1][j] = 3;

       }

    }

}

int cnt = 0;

for(int i=0;i<n;i++)

{

    for(int j=0;j<m;j++)

    {if(mat[i][j]==1 ||mat[i][j] == 2|| mat[i][j]==3)

    cnt++;

    // cout<<mat[i][j]<<" ";

    }

    // cout<<endl;

}

return cnt;

}

signed main()

{

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t; cin>>t; while(t--)

{

    cout<<solve()<<endl;    

}

return 0;

}

Test case

1
2 3
-1 1 -1
1 0 0

Output

3

Expected output

4

2 Likes

Thanks Brother!!

1 Like