在Java中通过迷宫回溯路径

在Java中通过迷宫回溯路径,java,arrays,queue,maze,Java,Arrays,Queue,Maze,因此,我有一个程序可以正确地解决迷宫问题,并将解决方案存储在多维整数数组solvedMaze中,如下所示: 110111 110101 010100 110100 011100 000000 指出起点和终点的位置: S10111 11010E 010100 110100 011100 000000 我必须求解和回溯迷宫路径的代码如下所示: public List<Location> solution() { int[][] solvedMaze = new int[hei

因此,我有一个程序可以正确地解决迷宫问题,并将解决方案存储在多维整数数组
solvedMaze
中,如下所示:

110111
110101
010100
110100
011100
000000
指出起点和终点的位置:

S10111
11010E
010100
110100
011100
000000
我必须求解和回溯迷宫路径的代码如下所示:

public List<Location> solution() {
    int[][] solvedMaze = new int[height][width];
    Location current;

    PriorityQueue<Location> queue = new PriorityQueue<>(new Comparator<Location>() {
        @Override
        public int compare(Location a, Location b) {
            double distanceA = distance(start, a) / 1.5 + distance(a, end);
            double distanceB = distance(start, b) / 1.5 + distance(b, end);
            return Double.compare(distanceA, distanceB);
        }

        private double distance(Location first, Location second) {
            return Math.abs(first.i() - second.i()) + Math.abs(first.j() - second.j());
        }
    });

    queue.add(start);

    while ((current = queue.poll()) != null) {
        if (solvedMaze[current.i()][current.j()] != 0) {
            continue;
        }

        int mod = 1;

        for (Location next : new Location[]{
            current.south(), current.west(), current.north(), current.east()
        }) {
            if (isInMaze(next) && isClear(next)) {
                if (solvedMaze[next.i()][next.j()] == 0) {
                    queue.add(next);
                } else {
                    mod = Math.min(solvedMaze[next.i()][next.j()], mod);
                }
            }
        }

        solvedMaze[current.i()][current.j()] = mod;

        if (isFinal(current)) {
            break;
        }
    }

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            System.out.print(solvedMaze[i][j]);
        }
        System.out.println();
    }

    if (solvedMaze[end.i()][end.j()] != 0) {
        List<Location> route = new ArrayList<>();
        Location temp = end;

        while (!temp.equals(start)) {
            route.add(temp);

            Location best = null;
            int bestNumber = solvedMaze[temp.i()][temp.j()];

            for (Location next : new Location[]{
                temp.north(), temp.south(), temp.west(), temp.east()
            }) {
                if (isInMaze(next) && solvedMaze[next.i()][next.j()] != 0) {
                    if (solvedMaze[next.i()][next.j()] < bestNumber) {
                        bestNumber = solvedMaze[next.i()][next.j()];
                        best = next;
                    }
                }
            }

            assert best != null;
            temp = best;
        }

        route.add(start);
        Collections.reverse(route);

        return route;
    } else {
        return null;
    }
}
公共列表解决方案(){
int[]solvedMaze=新int[高度][宽度];
位置电流;
PriorityQueue队列=新的PriorityQueue(新的比较器(){
@凌驾
公共整数比较(位置a、位置b){
双距离a=距离(起点,a)/1.5+距离(终点,a);
双距离b=距离(起点b)/1.5+距离(终点b);
返回Double.compare(距离A、距离B);
}
私人双倍距离(位置第一,位置第二){
返回Math.abs(first.i()-second.i())+Math.abs(first.j()-second.j());
}
});
queue.add(开始);
而((当前=queue.poll())!=null){
if(solvedMaze[current.i()][current.j()!=0){
继续;
}
int mod=1;
对于(位置下一步:新位置[]{
current.south(),current.west(),current.north(),current.east()
}) {
如果(isInMaze(下一个)和&isClear(下一个)){
if(solvedMaze[next.i()][next.j()]==0){
queue.add(下一步);
}否则{
mod=Math.min(solvedMaze[next.i()][next.j()],mod);
}
}
}
solvedMaze[current.i()][current.j()]=mod;
如果(最终(当前)){
打破
}
}
对于(int i=0;i

其中,
Location
是包含x和y坐标的类,
start
end
是位置。出于某种原因,我的输出总是
null
,我不知道为什么。经过一些简单的
print
调试,我发现回溯逻辑中的
solvedMaze[next.I()][next.j()]
条件从未被输入。这种方法有什么问题?有更好(更有效)的解决方法吗?

您显示的逻辑似乎依赖于
solvedMaze
上的“1”被替换为“从起点到终点的最短距离”,这就是为什么它在一条更好(也称为递减)数字的路径上从终点到终点反向遍历

例如,
solvedMaze
应为:

1  2  0 12 13 14
2  3  0 11  0 15
0  4  0 10  0  0
6  5  0  9  0  0
0  6  7  8  0  0
0  0  0  0  0  0

我改变了问题,把整个方法都包括进去;它既能解决迷宫问题,又能追溯其步骤。希望现在应该更清楚了!