Easiest to understand java solution


#1
public class Solution {
    static class Cell
    {
        int x, y, dis;
        Cell(int x, int y, int dis)
        {
            this.x = x;
            this.y = y;
            this.dis = dis;
        }
    }
    public int knight(int A, int B, int C, int D, int E, int F) {
        
        int dx[] = {-2, -1, 1, 2, -2, -1, 1, 2};  
        int dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};
        
        boolean vis[][] = new boolean[A + 1][B + 1];
        Queue<Cell> q=new LinkedList<>();
        q.add(new Cell(C, D, 0));
        vis[C][D] = true;
        int distance = -1;
        
        while(!q.isEmpty())
        {
            Cell t = q.poll();
            if(t.x == E && t.y == F)
            {
                distance = t.dis;
                break;
            }
            
            for(int i = 0; i < 8; i++)
            {
                int x = t.x + dx[i];
                int y = t.y + dy[i];
                
                if(isValid(x, y, A, B) && !vis[x][y])
                {
                    vis[x][y] = true;
                    q.add(new Cell(x, y, t.dis + 1));
                }
            }
            
        }
        return distance;
    }
    boolean isValid(int x, int y, int A, int B)
    {
        if(x >= 1 && x <= A && y >= 1 && y <= B)
            return true;
        
        return false;
    }
}