除了wasHere
数组存储解决方案(应该由correctPath
数组存储的事实外,我的迷宫求解器中包含了所有内容)。它也缺少标记迷宫的最终方块。应该存储的所有wasHere
数组都是程序在迷宫中进行的点。 correctPath
数组具有所有错误值,这是完全出乎意料的。我正在使用 Wikipedia 中提到的递归方法:https://en.wikipedia.org/wiki/Maze_solving_algorithm
这是我的 Maze Solver :
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;
}
答案 0 :(得分: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
内的recursiveSolve
方法的顶级调用之后移动main()
来电。像这样:
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);
}
如果这对你没有意义,那么这可能意味着你还没有完全掌握递归的工作原理。使用调试器来完成您的程序,就像您应该首先完成的那样,事情应该变得更加清晰。