【发布时间】:2019-06-19 23:29:09
【问题描述】:
我正在尝试编写一个递归解决迷宫的程序。迈出一步后,将在迷宫中的该位置放置一个“x”字符并打印迷宫,如果迷宫到达死胡同,它会通过从该位置删除“x”来回溯其最后的递归步骤。
运行时,程序总是停在第一步;它并没有完全解决迷宫
我试过了:
-硬编码每个连续的步骤以避免迷宫中的一堵墙(但这违背了使用递归的目的)
-开始第一步随机取(但这会导致ArrayIndexOutOfBoundsException)
import java.util.Random;
public class MazeTraversalWithRecursiveBacktracking
{
private static Random random = new Random();
public static void main(String args[])
{
char [][] maze = {{'#', '#', '#', '#', '#', '#','#', '#', '#', '#', '#', '#'},
{'#', '.', '.', '.', '#', '.','.', '.', '.', '.', '.', '#'},
{'.', '.', '#', '.', '#', '.','#', '#', '#', '#', '.', '#'},
{'#', '#', '#', '.', '#', '.','.', '.', '.', '#', '.', '#'},
{'#', '.', '.', '.', '.', '#','#', '#', '.', '#', '.', '.'},
{'#', '#', '#', '#', '.', '#','.', '#', '.', '#', '.', '#'},
{'#', '.', '.', '#', '.', '#','.', '#', '.', '#', '.', '#'},
{'#', '#', '.', '#', '.', '#','.', '#', '.', '#', '.', '#'},
{'#', '.', '.', '.', '.', '.','.', '.', '.', '#', '.', '#'},
{'#', '#', '#', '#', '#', '#','.', '#', '#', '#', '.', '#'},
{'#', '.', '.', '.', '.', '.','.', '#', '.', '.', '.', '#'},
{'#', '#', '#', '#', '#', '#','#', '#', '#', '#', '#', '#'}};
printMaze(maze);
mazeTraversal(maze, 2, 0);
}
public static void mazeTraversal(char [][] maze, int currX, int currY)
{
int choice = -1;
try
{
maze[currX][currY] = 'x';
printMaze(maze);
boolean chosen = false;
if ((currX == 4) && (currY == 11)) //end of the maze
{
System.out.println("Maze completed");
return;
}
while(!chosen)
{
choice = 1;
//System.out.println("Choice "+choice);
if (choice == 0)
{
if (maze[currX-1][currY] == '.')//up
{
System.out.println("Chose up");
chosen = true;
}
else
choice = random.nextInt(4);
}
else if (choice == 1)
{
if (maze[currX][currY+1] == '.')//right
{
System.out.println("Chose right");
chosen = true;
}
else
choice = random.nextInt(4);
}
else if (choice == 2)
{
if (maze[currX+1][currY] == '.')//down
{
System.out.println("Chose down");
chosen = true;
}
else
choice = random.nextInt(4);
}
else if (choice == 3)
{
if (maze[currX][currY-1] == '.')//left
{
System.out.println("Chose left");
chosen = true;
}
else
choice = random.nextInt(4);
}
else
{
System.out.println("Haven't chosen");
choice = random.nextInt(4);
}
//System.out.println(choice+" "+chosen);
}
System.out.println(choice+" "+chosen);
if (choice == 0)
mazeTraversal(maze,currX-1,currY);
else if (choice == 1)
mazeTraversal(maze,currX,currY+1);
else if (choice == 2)
mazeTraversal(maze,currX+1,currY);
else if (choice == 3)
mazeTraversal(maze,currX,currY-1);
else //backup
{
recursiveBacktrack(maze, currX, currY);
}
}
catch(ArrayIndexOutOfBoundsException ex)
{
ex.printStackTrace();
System.out.println("Maze finished with choice = "+choice);
}
}
public static void recursiveBacktrack(char [][]maze, int currX, int currY)
{
maze[currX][currY] = ' ';
}
public static void printMaze(char maze[][])
{
for(int i = 0; i < 12; ++i)
{
for(int j = 0; j < 12; ++j)
{
System.out.print(maze[i][j]+" ");
}
System.out.println();
}
System.out.println();
System.out.println();
}
}
预期结果:预期结果是通过在每个递归步骤之后重新打印整个迷宫,递归地解决迷宫,显示每次尝试。 '#' 是一堵墙,'.'是一个空闲空间,“x”是一个已被占用的空间。
实际结果:如前所述,我得到的实际结果只是程序无限循环的第一个递归步骤。
错误消息:有时,我会收到错误消息 ArrayIndexOutOfBoundsException: -1
【问题讨论】:
-
你的choice=1需要在while循环之前,否则它总是等于1
-
为了好玩,我创建了自己的求解器:ideone.com/VvHsB2
-
@Ehcnalb 你是对的,它有效
标签: java recursion recursive-backtracking