Simplest ever JAVA BFS


#1

Comment body goes here.`public class Solution {
class Point
{
int r,c;

    Point(int a,int b)
    {
        r=a;
        c=b;
    }
    
}

public int knight(int A, int B, int C, int D, int E, int F) 
{
    boolean vis[][]=new boolean[A+1][B+1];
    int dis[][]=new int [A+1][B+1];
    
    int r[]={-2 ,-1 ,1, 2, 2, 1 ,-1 ,-2};
    int c[]={-1 ,-2 ,-2 ,-1, 1 ,2 ,2 ,1};
    
    Queue<Point> que=new LinkedList<Point>();
    que.add(new Point(C,D));
    vis[C][D]=true;
    
    while(!que.isEmpty())
    {
        Point rm=que.poll();
        
        if(rm.r==E && rm.c==F)
        {
            return dis[rm.r][rm.c];
        }
        
        for(int i=0;i<8;i++)
        {
            int nr=rm.r+r[i];
            int nc=rm.c+c[i];
            if(nr>=1 && nc>=1 && nr<= A && nc<=B && vis[nr][nc]==false)
            {
                que.add(new Point(nr,nc));
                vis[nr][nc]=true;
                dis[nr][nc]=dis[rm.r][rm.c]+1;
            }
        }
    }
    return -1;
}

}
`