Knight On Chess Board

Given any source point, (C, D) and destination point, (E, F) on a chess board, we need to find whether Knight can move to the destination or not.

The above figure details the movements for a knight ( 8 possibilities ).

If yes, then what would be the minimum number of steps for the knight to move to the said point.
If knight can not move from the source point to the destination point, then return -1.

Note: A knight cannot go out of the board.


Input Format:

The first argument of input contains an integer A.
The second argument of input contains an integer B.
    => The chessboard is of size A x B.
The third argument of input contains an integer C.
The fourth argument of input contains an integer D.
    => The Knight is initially at position (C, D).
The fifth argument of input contains an integer E.
The sixth argument of input contains an integer F.
    => The Knight wants to reach position (E, F).

Output Format:

If it is possible to reach the destination point, return the minimum number of moves.
Else return -1.

Constraints:

1 <= A, B <= 500

Example

Input 1:
    A = 8
    B = 8
    C = 1
    D = 1
    E = 8
    F = 8
    
Output 1:
    6

Explanation 1:
    The size of the chessboard is 8x8, the knight is initially at (1, 1) and the knight wants to reach position (8, 8).
    The minimum number of moves required for this is 6.
Solution:

Time: O(VE)

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 n = (Node) o;
            return x == n.x && y == n.y;
        }
        
        public boolean valid(int m, int n) {
            return x >= 1 && x <= m && y >= 1 && y <= n;
        }
    }
    
    public int knight(int A, int B, int C, int D, int E, int F) {
        int[] dx = new int[]{-1,-2,-2,-1,1,2,2,1};
        int[] dy = new int[]{-2,-1,1,2,2,1,-1,-2};
        Node start = new Node(C, D);
        Deque<Node> queue = new ArrayDeque<>();
        Set<Node> visited = new HashSet<>();
        queue.offer(start);
        int step = -1;
        while (!queue.isEmpty()) {
            step ++;
            int size = queue.size();
            for (int i = 0; i < size; i ++) {
                Node curr = queue.poll();
                if (visited.add(curr)) {
                    if (curr.x == E && curr.y == F) {
                        return step;
                    }
                    for (int j = 0; j < 8; j ++) {
                        Node next = new Node(curr.x + dx[j], curr.y + dy[j]);
                        if (next.valid(A, B)) {
                            queue.offer(next);
                        }
                    }
                }
            }
        }
        return -1;
    }
}