Java 递归迷宫求解方法
给定一个用0和1填充的二维字符数组,其中0表示墙,1表示有效路径,我开发了一种称为findPath(int r,int c)的递归方法,用于在标有“x”的迷宫中找到出口。该方法接收迷宫的当前行和列,并通过N、E、S、W方向,直到找到有效路径并用“+”标记该有效路径。如果发现所有方向都被墙挡住,则该方法假设回溯,直到情况不再如此,然后用“F”标记该路径,以表示坏路径 现在我不明白为什么findPath方法似乎没有横穿所有方向,因为我的显示方法只是显示从我传入的坐标开始的程序,没有从那里移动,为什么会这样 这是我的驾驶课Java 递归迷宫求解方法,java,arrays,recursion,Java,Arrays,Recursion,给定一个用0和1填充的二维字符数组,其中0表示墙,1表示有效路径,我开发了一种称为findPath(int r,int c)的递归方法,用于在标有“x”的迷宫中找到出口。该方法接收迷宫的当前行和列,并通过N、E、S、W方向,直到找到有效路径并用“+”标记该有效路径。如果发现所有方向都被墙挡住,则该方法假设回溯,直到情况不再如此,然后用“F”标记该路径,以表示坏路径 现在我不明白为什么findPath方法似乎没有横穿所有方向,因为我的显示方法只是显示从我传入的坐标开始的程序,没有从那里移动,为什么
public class MazeMain2
{
public static void main(String[]args)
{
char[][] mazeArr = {{'0','0','0','1','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','1','0','0','0','0','1','0','0','0','0','1','0'},
{'0','0','0','1','1','1','1','1','1','1','1','1','0','0','0'},
{'0','0','0','1','0','0','0','0','0','0','0','1','0','0','0'},
{'0','0','0','1','1','1','1','1','0','0','0','1','0','0','0'},
{'0','0','0','0','0','0','0','1','0','0','0','1','0','0','0'},
{'0','0','0','0','1','1','1','1','0','0','0','1','0','0','0'},
{'0','0','0','0','1','0','0','1','0','0','0','1','0','1','0'},
{'0','0','0','0','1','0','0','1','0','0','0','0','0','0','0'},
{'0','0','0','0','1','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','1','1','1','1','1','1','1','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'},
{'0','0','0','0','0','1','0','0','0','0','1','1','1','1','0'},
{'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'}};
MazeSolver2 mazeS = new MazeSolver2(mazeArr);
mazeS.markEntry();
mazeS.markExit();
mazeS.solve(0, mazeS.start);
}
}
这是我的迷宫解算器类,使用findPath方法
public class MazeSolver2
{
int start;
int exit;
char[][] maze;
public MazeSolver2(char[][] currentMaze)
{
maze = currentMaze;
}
//Finds where the first 1 is in the top row of the
//maze (entrance)
public void markEntry()
{
for(int x = 0; x < maze.length; x++)
{
if(maze[0][x] == '1')
{
maze[0][x] = 'E';
start = x;
}
}
}
//Finds where the last 1 is in the bottom row of the
//maze (exit)
public void markExit()
{
for(int x = 0; x < maze.length; x++)
{
if(maze[maze.length - 1][x] == '1')
{
maze[maze.length - 1][x] = 'x';
exit = x;
}
}
}
public void solve(int x, int y)
{
if(findPath(x, y))
{
System.out.println(maze[x][y]);
}
else
System.out.println("No solution");
}
public boolean findPath(int r, int c)
{
displayMaze(maze);
//Found the exit
if(maze[r][c] == 'x')
{
return true;
}
if(maze[r][c] == '0' || maze[r][c] == '+' || maze[r][c] == 'F')
{
return false;
}
maze[r][c] = '+';
//If row is currently at zero then don't check north
//direction because it will be outside of the maze
if(r <= 0)
{
if(findPath(r, c++))
{
return true;
}
if(findPath(r++, c))
{
return true;
}
if(findPath(r, c--))
{
return true;
}
}
else
{
//check N, E, S, W directions
if(findPath(r--, c) || findPath(r, c++) ||
findPath(r++, c) || findPath(r, c--))
{
return true;
}
}
//Marking the bad path
maze[r][c] = 'F';
return false;
}
//Displays maze
public void displayMaze(char[][] maze)
{
for(int row = 0; row < maze.length; row++)
{
for(int col = 0; col < maze.length; col++)
{
if(col == 14)
{
System.out.print(maze[row][col]);
System.out.println();
}
else
{
System.out.print(maze[row][col]);
}
}
}
System.out.println();
}
}
公共类MazeSolver2
{
int启动;
int出口;
char[][]迷宫;
公共迷宫解决方案2(字符[][]当前迷宫)
{
迷宫=电流迷宫;
}
//查找第一个1在列表顶行中的位置
//迷宫(入口)
公共无效标记条目()
{
对于(int x=0;x 如果(r您的算法本身有几个流,我觉得指出这些流是不对的。您可以搜索迷宫遍历问题,并获得许多好的教程
但是,请注意方法调用。请注意,如果使用findPath(intr,intc)
调用findPath(5,5)
,则对findPath(r,c++)
的调用再次传递值findPath(5,5)
,而不是使用findPath(5,6)
因为在这种情况下,findPath(r,c++)
使用当前值c
调用,然后执行c++
这同样适用于findPath(r,c--)findPath(r++,c)
等
了解这一事实的一个好办法是在方法findPath()
的开头打印值intr,intc
。
也可以玩一些后增量/减量(x++/--x)和前增量/减量(++x/--x)
希望有帮助。您是的受害者。始终在新行上递增。仅供参考-如果您的入口点位于(0,0),您的代码将引发异常。请查看if(findPath(r,c-)