Help needed for Comfortable cows problem USACO silver Flood fill

Problem Info

Comfortable cows + USACO

My Work

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define all(v) v.begin(),v.end()
#define umap unordered_map

int mod=1000000007;
int ans=0;
void prop(vector<vector>&mat,int x,int y){

	if(mat[x-1][y]==0 and mat[x+1][y]!=0 and mat[x][y+1]!=0 and mat[x][y-1]!=0){
		mat[x-1][y]=1;
		ans++;
		prop(mat,x-1,y);
		prop(mat,x-2,y);
		prop(mat,x-1,y-1);
		prop(mat,x-1,y+1);
		
	}
	
	if(mat[x-1][y]!=0 and mat[x+1][y]==0 and mat[x][y+1]!=0 and mat[x][y-1]!=0){
			mat[x+1][y]=1;
			ans++;
		prop(mat,x+1,y);
		prop(mat,x+2,y);
		prop(mat,x+1,y-1);
		prop(mat,x+1,y+1);
	}
	
	if(mat[x-1][y]!=0 and mat[x+1][y]!=0 and mat[x][y+1]==0 and mat[x][y-1]!=0){
			mat[x][y+1]=1;
			ans++;
			prop(mat,x,y+1);
		prop(mat,x,y+2);
		
		prop(mat,x-1,y+1);
		prop(mat,x+1,y+1);
	}
	
	if(mat[x-1][y]!=0 and mat[x+1][y]!=0 and mat[x][y+1]!=0 and mat[x][y-1]==0){
			mat[x][y-1]=1;
			ans++;
		prop(mat,x,y-1);
		prop(mat,x,y-2);
		prop(mat,x-1,y-1);
		prop(mat,x+1,y-1);
	}

}

void solve(){
int n;
cin>>n;
vector<vector>mat(3000,vector(3000,0));
int x,y;

while(n--){
	cin>>x>>y;
	x+=1000;
	y+=1000;
	if(mat[x][y]==1){
		ans--;
		
	}
	mat[x][y]=1;
	prop(mat,x,y);
	prop(mat,x-1,y);
	prop(mat,x,y-1);
	prop(mat,x+1,y);
	prop(mat,x,y+1);
	
	
	cout<<ans<<"\n";

	
}

}
int main()
{ios_base::sync_with_stdio(false);cin.tie(0);cin.tie(0);
//freopen(“SKS.in”, “r”, stdin);
//freopen(“SKS.out”, “w”, stdout);
int t=1;
//cin>>t;
while(t–)solve();

return 0;}

Add commented code here
I donot know why I am geting wrong answer for all test case except sample test case.
Add explanation of code here
I used dfs to solve code.
My code clearly explain my logic.

What I’ve Tried

I donot know that is it possible to download test data in USACO .
I anyone know please reply.

1 Like

Please format your code correctly as described at: How to ask for help on a problem - #2. Also read the section titled "If you need help debugging your code:.

Um USACO

1 Like