有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

迷宫算法的java数组未存储正确的值

除了wasHere数组正在存储解决方案(应该由correctPath数组存储)这一事实之外,我的迷宫解算器中有所有内容。它也没有标记迷宫的末端正方形。所有的wasHere数组都应该存储程序在迷宫中找到的点。correctPath数组包含所有假值,这是完全意外的。我正在使用维基百科中提到的递归方法:https://en.wikipedia.org/wiki/Maze_solving_algorithm

这是我的迷宫解算器

private static int[][] maze = {{2, 2, 2, 2, 1, 2, 2}, 
                               {2, 2, 2, 2, 1, 2, 2}, 
                               {2, 2, 2, 2, 1, 2, 2}, 
                               {2, 1, 1, 1, 1, 1, 1}}; // The maze
private static boolean[][] wasHere = new boolean[4][7];
private static boolean[][] correctPath = new boolean[4][7]; // Solution

private static int startX = 4;
private static int startY = 0;

private static int endX = 1;
private static int endY = 3;

public static void main(String[] args) {
    System.out.println("Maze: ");
    printMaze(maze);

    solveMaze();

    boolean b = recursiveSolve(startX, startY); // Whether or not there is a solution to the maze
}

public static void solveMaze()
{
    for (int row = 0; row < maze.length; row++)
    {
        // Sets boolean arrays to false
        for (int col = 0; col < maze[row].length; col++)
        {
            wasHere[row][col] = false;
            correctPath[row][col] = false;
        }
    }
}

public static void printMaze(int[][] array)
{
    for (int row = 0; row < array.length; row++)
    {
        for (int col = 0; col < array[row].length; col++)
        {
            System.out.print(array[row][col]);

            if (col == array[row].length - 1)
            {
                System.out.print("\n");
            }
        }
    }       

    System.out.print("\n");
}

public static void printPath(boolean[][] array)
{
    for (int row = 0; row < array.length; row++)
    {
        for (int col = 0; col < array[row].length; col++)
        {
            if (array[row][col] == true)
            {
                System.out.print("1");
            }
            else
            {
                System.out.print("2");
            }

            if (col == array[row].length - 1)
            {
                System.out.print("\n");
            }
        }
    }
}

public static boolean recursiveSolve(int x, int y)
{
    if (x == endX && y == endY) // Reach end
    {
        System.out.println("The maze is solvable.");
        printPath(wasHere);
        return true;
    }

    if (maze[y][x] == 2 || wasHere[y][x] == true) // Hit a dead end or end up in same place (no solution)
    {
        return false;
    }

    wasHere[y][x] = true;

    if (x != 0) // On left edge or not
    {
        if (recursiveSolve(x - 1, y))
        {
            correctPath[y][x] = true;
            return true;
        }
    }

    if (x != maze[0].length - 1) // On right edge or not
    {
        if (recursiveSolve(x + 1, y))
        {
            correctPath[y][x] = true;
            return true;
        }
    }

    if (y != 0) // On top edge or not
    {
        if (recursiveSolve(x, y - 1))
        {
            correctPath[y][x] = true;
            return true;
        }
    }

    if (y != maze.length - 1) // On bottom edge or not
    {
        if (recursiveSolve(x, y + 1))
        {
            correctPath[y][x] = true;
            return true;
        }
    }

    System.out.println("The maze is not solvable.");
    return false;
}

共 (1) 个答案

  1. # 1 楼答案

    您的迷宫解算器工作正常。问题是,在递归方法完成对correctPath数组的写入之前,您可能正在打印该数组的值

    我假设recursiveSolve(int x, int y)方法中有以下行:

        System.out.println("The maze is solvable.");
        printPath(wasHere);
    

    。。。在某个时候,您尝试使用correctPath变量来运行它,对吗?像这样的

        System.out.println("The maze is solvable.");
        printPath(correctPath);
    

    但这还为时过早。correctPath数组值在递归调用从迷宫末尾开始返回后设置

    相反,尝试将printPath调用移动到main()内的recursiveSolve方法的顶层调用之后。像这样:

    public static void main(String[] args) {
        System.out.println("Maze: ");
        printMaze(maze);
    
        solveMaze();
    
        boolean b = recursiveSolve(startX, startY); // Whether or not there is a solution to the maze
    
        // Put this here!  It will work as expected.
        System.out.println();
        printPath(correctPath);
    }
    

    如果这对您来说不太有意义,那么可能意味着您还没有完全掌握递归的工作原理。使用调试器一步一步地完成程序,这是您首先应该做的,事情应该会变得更清楚