Best Solution you'll saw


#1
int dx[8] = {0,1,0,-1,1,1,-1,-1};
int dy[8] = {-1,0,1,0,-1,1,1,-1};
int dist(int x1, int y1, int x2, int y2){
    return ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
void dfsGraph(vector<vector<int>> &v, int start, int end, int x, int y, int r){
    if(x < 0 || y< 0 || x>=v.size() || y>=v[0].size() || dist(x,y,start,end)>r*r || v[x][y]==-1)
        return;
    v[x][y] = -1;
    for(int i=0;i<8;i++){
        dfsGraph(v, start, end, x+dx[i], y+dy[i], r);
    }
}
bool dfs(vector<vector<int>> &v, int endx, int endy, int x, int y){
    if(x < 0 || y< 0 || x>=v.size() || y>=v[0].size() || v[x][y]==-1 || v[x][y]==1)
        return false;
    if(x==endx && y==endy)
        return true;
    v[x][y]=1;
    bool curr = false;
    for(int i=0;i<8;i++){
        curr = curr || dfs(v, endx, endy, x+dx[i], y+dy[i]);
        if(curr)
            return true;
    }
    return curr;
}
void print(vector<vector<int>> &v){
    int count = 0;
    for(auto e : v){
        for(auto x : e){
            if(x==-1)
                count++;
            cout<<x<<" ";
        }
        cout<<endl;
    }
}
string Solution::solve(int x, int y, int n, int r, vector<int> &xs, vector<int> &ys) {
    vector <vector<int>> v(x+1, vector<int>(y+1, 0));
    vector <pair<int, int>> circles;
    for(int i=0;i<n;i++)
        circles.push_back({xs[i], ys[i]});
    sort(circles.begin(), circles.end());
    for(auto e : circles)
        dfsGraph(v, e.first, e.second, e.first, e.second, r);
    // print(v);
    return dfs(v, x, y, 0, 0) ? "YES":"NO";
}

#2

Can u tell that why u sort circles.