Valid Path

There is a rectangle with left bottom as  (0, 0) and right up as (x, y). There are N circles such that their centers are inside the rectangle.
Radius of each circle is R. Now we need to find out if it is possible that we can move from (0, 0) to (x, y) without touching any circle.

Note : We can move from any cell to any of its 8 adjacent neighbors and we cannot move outside the boundary of the rectangle at any point of time.


Input Format

1st argument given is an Integer x.
2nd argument given is an Integer y.
3rd argument given is an Integer N, number of circles.
4th argument given is an Integer R, radius of each circle.
5th argument given is an Array A of size N, where A[i] = x coordinate of ith circle
6th argument given is an Array B of size N, where B[i] = y coordinate of ith circle

Output Format

Return YES or NO depending on weather it is possible to reach cell (x,y) or not starting from (0,0).

Constraints

0 <= x, y, R <= 100
1 <= N <= 1000
Center of each circle would lie within the grid

For Example

Input:
    x = 2
    y = 3
    N = 1
    R = 1
    A = [2]
    B = [3]
Output:
    NO
   
Explanation:
    There is NO valid path in this case
Solution:

Time: O(xyN)

public class Solution {
    static class Node {
        int x;
        int y;
        public Node(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
        @Override
        public int hashCode() {
            int hash = 31;
            hash += 17 * x;
            hash += 17 * y;
            return hash;
        }
        
        @Override
        public boolean equals(Object o) {
            Node b = (Node) o;
            return b.x == x && b.y == y;
        }
        
        public double distance(Node n) {
            int a = Math.abs(n.x - x);
            int b = Math.abs(n.y - y);
            return Math.sqrt(a * a + b * b);
        }
        
        public boolean valid(int m, int n) {
            return x >= 0 && x <= m && y >= 0 && y <= n;
        }
        
        public boolean inRange(Node center, int R) {
            double r = (double) R;
            return Double.compare(distance(center), r) <= 0;
        }
    }
    
    public String solve(int A, int B, int C, int D, int[] E, int[] F) {
        Deque<Node> queue = new ArrayDeque<>();
        Set<Node> visited = new HashSet<>();
        int[] dx = new int[]{-1, -1, 0, 1, 1, 1, 0, -1};
        int[] dy = new int[]{0, 1, 1, 1, 0, -1, -1, -1 };
        Node start = new Node(0, 0);
        queue.add(start);
        while (!queue.isEmpty()) {
            Node curr = queue.poll();
            if (visited.add(curr)) {
                if (curr.x == A && curr.y == B) {
                    return "YES";
                }
                for (int i = 0; i < 7; i ++) {
                    Node next = new Node(curr.x + dx[i], curr.y + dy[i]);
                    if (next.valid(A, B)) {
                        boolean inRange = false;
                        for (int j = 0; j < C; j ++) {
                            Node circle = new Node(E[j], F[j]);
                            if (next.inRange(circle, D)) {
                                inRange = true;
                                break;
                            }
                        }
                        if (!inRange) {
                            queue.offer(next);
                        }
                    }
                }
            }
        }
        return "NO";
    }
}