Java 骑士的最短路径(BFS)

Java 骑士的最短路径(BFS),java,breadth-first-search,Java,Breadth First Search,请帮助我了解我的代码有哪些错误。我正在尝试使用BFS获得最短路径来解决问题,但它要么给我-1要么给我2。答案应该是6。我做错了什么?这就是问题所在: 给定一个棋盘,找出骑士从给定来源到达给定目的地的最短距离(最少步数) 例如,N=8(8 x 8板),源=(7,0)目标=(0,7) 所需的最少步骤数为6 我的代码如下: class Point { int x, y; public Point(int x, int y){ this.x = x; this.y = y

请帮助我了解我的代码有哪些错误。我正在尝试使用BFS获得最短路径来解决问题,但它要么给我-1要么给我2。答案应该是6。我做错了什么?这就是问题所在:

给定一个棋盘,找出骑士从给定来源到达给定目的地的最短距离(最少步数)

例如,N=8(8 x 8板),源=(7,0)目标=(0,7)

所需的最少步骤数为6

我的代码如下:

class Point {
    int x, y;
    public Point(int x, int y){
    this.x = x;
    this.y = y;
 }
}

class knightShortestPath {
    int N = 8;
    public static boolean visited[][];

public boolean isPositionValid(int x, int y){
    if( x < 0 || y < 0 || x > this.N || y > this.N){
        return false;
    }
    return true;
}

public void createChessBoard(int N) {
    this.N = N;
    visited = new boolean[this.N][this.N];
    for (int i = 0; i < this.N; i++) {
        for (int j = 0; j < this.N; j++) {
            visited[i][j] = false;
        }
    }
}

public int BFS(Point source, Point destination) {
    int row[] = {2, 2, -2, -2, 1, 1, -1, -1};
    int col[] = {1, -1, 1, -1, 2, -2, 2, -2};
    Queue<Point> queue = new LinkedList<>();
    queue.offer(source);
    visited[source.x][source.y] = true;
    int minimumNumSteps = 0;

    while (!queue.isEmpty()) {
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            Point pt = queue.poll();
            if (pt.x == destination.x && pt.y == destination.y) {
                return minimumNumSteps;
            }
            for (int j = 0; j < size; j++) {
                Point next = new Point(pt.x + row[i], pt.y + col[j]);
                if (isPositionValid(pt.x + row[i], pt.y + col[j]) && !visited[i][j]) {
                    visited[i][j] = true;
                    queue.offer(next);
                }
            }
        }
        minimumNumSteps++;
    }
    return minimumNumSteps;
}


public static void main(String[] args) {
    knightShortestPath position = new knightShortestPath();
    position.createChessBoard(8);
    Point src = new Point(0,7);
    Point dest = new Point(7,0);
    System.out.println("The minimum number of steps are: " + position.BFS(src, dest)); //answer is 6
 }
}
类点{
int x,y;
公共点(整数x,整数y){
这个.x=x;
这个。y=y;
}
}
骑士类最短路径{
int N=8;
访问的公共静态布尔值[];
公共布尔值isPositionValid(整数x,整数y){
如果(x<0 | | y<0 | | x>this.N | | y>this.N){
返回false;
}
返回true;
}
公共棋盘(整数N){
这个,N=N;
visited=新布尔值[this.N][this.N];
for(int i=0;i
第一件事:我不知道你怎么会得到负值。在使用0初始化后,绝不会减少
最小numsteps
。可能是溢流吗?我觉得很奇怪

此外,我看到两个问题:

  • 两个for循环不正确。您当前正在迭代
    队列.size()
    。相反,您要做的是迭代当前节点的所有子节点
  • 轮询for循环外部的当前点
  • 因此:

    while(!queue.isEmpty()){
    Point pt=queue.poll();
    //检查它是否是目标
    // ...
    for(int i=0;i
    另一个注意事项:当队列为空且尚未达到目标时,没有解决方案。目前,您正在返回一些可能被错误解释的值

    while(!queue.isEmpty()) {
        Point pt = queue.poll();
        // check if it's target 
        // ...
        for (int i = 0; i < row.length; i++) {
            // ... 
            for (int j = 0; j < col.length; j++) {
                // ...
            }
        }
    }