C++ || dfs || o(n*m)


#1

vector dx = { 1, -1, 0, 0, 1, 1, -1, -1};
vector dy = { 0, 0, 1, -1, 1, -1, 1, -1};

void dfs(int i, int j, int X, int Y, vector<vector>& grid, vector<vector>& visited){
if(i < 0 || i > X || j < 0 || j > Y || visited[i][j] || grid[i][j] == 1)
return;

visited[i][j] = true;

for(int k = 0; k < 8; k++){
    dfs(i+dx[k],j+dy[k],X,Y,grid,visited);
}

}

string Solution::solve(int X, int Y, int N, int R, vector &A, vector &B) {
vector<vector> grid(X+1,vector(Y+1,0));
vector<vector> visited(X+1,vector(Y+1,false));

for(int i = 0; i <= X; i++){
    for(int j = 0; j <= Y; j++){
        for(int k = 0; k < N; k++){
            if(sqrt(pow(A[k]-i,2)+pow(B[k]-j,2)) <= R){
                grid[i][j] = 1;
                break;
            }
        }
    }
}

if(grid[0][0] == 1 || grid[X][Y] == 1) return "NO";

dfs(0,0,X,Y,grid,visited);

if(visited[X][Y]) 
    return "YES";
    
return "NO";

}