Cpp DFS solution


#1

void recur(int x, int y, int N, int R, vector &A, vector &B,int i, int j, int &ans, vector<vector > &dp){

if(ans == 1)return;
if(i>x or j>y or i<0 or j<0)return;
if(dp[i][j]!=-1)return;
for(int k = 0 ;k<N;k++){
    if(((A[k]-i)*(A[k]-i) + (B[k]-j)*(B[k]-j))<=(R*R))return;
}
if(i == x and j == y){ans = 1; return;}
dp[i][j]=1;
recur(x,y,N,R,A,B,i+1,j,ans,dp);
recur(x,y,N,R,A,B,i,j+1,ans,dp);
recur(x,y,N,R,A,B,i+1,j+1,ans,dp);
recur(x,y,N,R,A,B,i-1,j,ans,dp);
recur(x,y,N,R,A,B,i,j-1,ans,dp);
recur(x,y,N,R,A,B,i-1,j-1,ans,dp);
return;

}

string Solution::solve(int x, int y, int N, int R, vector &A, vector &B) {
int ans = 0;
vector<vector > dp(x+1,(vector(y+1,-1)));
recur(x,y,N,R,A,B,0,0,ans,dp);
if(ans)return “YES”;
else return “NO”;
}


#2

your soln. is amzing loved it.
But why did you recur for only six direction.