bfs迷宫解算器也返回false

fdbelqdn  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(359)

我目前正在做一个小的迷宫求解项目,你可以在迷宫中找到最短路径,以便更好地掌握路径搜索算法;在这种情况下,广度优先搜索算法。它的工作原理是使用布尔访问矩阵来标记访问的单元格,以避免重复步骤,它的工作原理如下。
第1步:使用访问队列来容纳相邻的单元格。
步骤2:删除队列前面的单元格并将其添加到访问列表中。
第三步:检查相邻的单元,如果它们不是墙,也没有被访问,它们将被添加到访问队列中。
然后重复最后两个步骤,直到整个队列为空。
然而,奇怪的是,我的迷宫解算器一直返回false,即使它已经找到了自己的方式结束(即数字3)。总之,我有两个问题,是什么导致它返回假,即使它找到了,什么可以在解算器中更改,以便只有最短路径将有数字2(即当它回溯时,它将把死端路径变回0)?
提前谢谢!
bfs迷宫求解器

import java.util.LinkedList;
import java.util.Queue;

public class BFS {

    // Solves given maze iteratively, input starting position in maze and size of the maze.
    public static boolean solve(int[][] maze, int x, int y, int sizeX, int sizeY) {

        boolean[][] visited = new boolean[sizeX][sizeY];
        Queue<Point> vQueue = new LinkedList<Point>();
        vQueue.add(new Point(x, y, null));
        maze[x][y] = 2;
        visited[x][y] = true;

        while (!vQueue.isEmpty()) {

            Point p = vQueue.remove();

            // 3 is the cell the algorithm is supposed to find.
            if (maze[p.x][p.y] == 3) {
                maze[p.x][p.y] = 2;
                return true;
            }

            // Down.
            if (visited[p.x-1][p.y] == false && (maze[p.x-1][p.y] == 0 || maze[p.x-1][p.y] == 3)) {
                maze[p.x-1][p.y] = 2;
                visited[p.x-1][p.y] = true;
                Point nextPoint = new Point(p.x-1, p.y, p);
                vQueue.add(nextPoint);
            }

            // Up.
            if (visited[p.x+1][p.y] == false  && (maze[p.x+1][p.y] == 0 || maze[p.x+1][p.y] == 3)) {
                maze[p.x+1][p.y] = 2;
                visited[p.x+1][p.y] = true;
                Point nextPoint = new Point(p.x+1, p.y, p);
                vQueue.add(nextPoint);
            }

            // Right.
            if (visited[p.x][p.y+1] == false  && (maze[p.x][p.y+1] == 0 || maze[p.x][p.y+1] == 3)) {
                maze[p.x][p.y+1] = 2;
                visited[p.x][p.y+1] = true;
                Point nextPoint = new Point(p.x, p.y+1, p);
                vQueue.add(nextPoint);
            }

            // Left.
            if (visited[p.x][y-1] == false  && (maze[p.x][p.y-1] == 0 || maze[p.x][p.y-1] == 3)) {
                maze[p.x][p.y-1] = 2;
                visited[p.x][p.y-1] = true;
                Point nextPoint = new Point(p.x, p.y-1, p);
                vQueue.add(nextPoint);
            }
        }

        return false;
    }

    // Node class that holds current position and visitation list.
    private static class Point{
        int x;
        int y;
        Point parent;

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

    }

}

测试迷宫

public class TestMazeDFS {
    private static int[][] maze = {
            {1, 1, 1, 1, 1, 1, 1, 1, 1},        
            {1, 0, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 1, 0, 1, 1, 1, 1, 1},
            {1, 0, 1, 0, 0, 0, 0, 0, 1},
            {1, 1, 1, 1, 1, 0, 1, 0, 1},
            {1, 0, 0, 0, 1, 0, 1, 0, 1},
            {1, 0, 1, 1, 1, 1, 1, 0, 1},
            {1, 0, 0, 0, 0, 0, 0 ,3, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1},
        };

    public int[][] getMaze(){

        return this.maze;
    }

    // prints the maze.
    public static void printMaze() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
//              if (maze[i][j] == 1) {
//                  System.out.print('#');
//              } else {
//                  System.out.print(' ');
//              }
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args) {
        TestMazeDFS maze = new TestMazeDFS();
        boolean test = BFS.solve(maze.getMaze(), 1, 1, 9, 9);
        System.out.println(test);
        printMaze();

    }
}
i1icjdpr

i1icjdpr1#

问题似乎是你改变了 maze 在探索节点之前。
最简单的例子是源和目标是同一点的图。
您首先设置此点: maze[x][y] = 2 -只有这样你才能探索它,并检查它的价值 3 . 但现在已经不是这样了。
要解决这个问题,应该设置 maze[x'][y']2 仅当您浏览节点时,而不是将其添加到队列时。

相关问题