[英]Maze recursion solving StackOverflow error
我正在尝试使用递归来解决迷宫问题。 它被宣布为Cell [][] maze
。
public class Cell {
private Wall left;
private Wall right;
private Wall up;
private Wall down;
private boolean end;
// Setters and getters not shown
}
如果单元格的某一侧没有Wall
,那么它的值为null
,否则它指的是Wall
对象。 Wall
参考是一致的:与单壁相邻的两个单元都用适当的字段表示它。 如果缺少墙,则两个相邻单元都具有相应的null
条目。 这是搜索:
public boolean solveMaze(Cell[][] maze, int i, int j) {
if (maze[i][j].isEnd()){
System.out.println(maze[i][j].toString());
return true;
}
if (maze[i][j].getDown() == null) {
return solveMaze(maze, i, j + 1);
}
if (maze[i][j].getUp() == null) {
return solveMaze(maze, i, j - 1) ;
}
if (maze[i][j].getLeft() == null) {
return solveMaze(maze, i - 1, j);
}
if (maze[i][j].getRight() == null) {
return solveMaze(maze, i + 1, j) ;
}
return false;
}
我收到了Stack Overflow
错误。 我的递归停止条件有什么问题?
更新:
有了您非常感谢的帮助,我解决了这个问题:这是正确无误的解决方案:
public boolean solveMaze(Cell[][] maze, int i, int j){
if (maze[i][j].isEnd()){
System.out.println("Maze Exit :["+i+","+j+"]" );
return true;
}
if (maze[i][j].isVisited()){
return false;
}
maze[i][j].setVisited(true);
if ((maze[i][j].getButtom() == null) ){
if (solveMaze(maze,i,j+1)==true)
return true;
}
if ((maze[i][j].getUp() == null) ){
if ( solveMaze(maze,i,j-1) ==true )
return true;
}
if ((maze[i][j].getLeft() == null)){
if (solveMaze(maze,i-1,j))
return true;
}
if ((maze[i][j].getRight() == null)){
if (solveMaze(maze,i+1,j))
return true;
}
maze[i][j].setVisited(false);
return false;
}
可能对未来的任何机构都有帮助。
如果迷宫有一个循环,求解器可以永远围绕这个循环运行,这将导致你看到的堆栈溢出。 您需要一种方法来确定何时看到已经看过的迷宫广场。 在这种情况下,您应该立即回溯。
这可以通过在最初设置为false的每个单元格中visited
的布尔标志然后为您搜索的每个方块设置为true来完成,或者您可以维护已搜索的单独的一Set
(i,j)
对,最初为空。
注意:你对i
和j
使用是非常规的。 如果其他人用常规用法编写了迷宫阅读代码,这可能会导致问题。 在数学中, i
通常用于行号, j
用于列。 根据此惯例,您的墙壁测试不同意您的增量和减量。 如果缺少底壁,则需要增加i
。
在我看来,就像你在解算器方法中运行一样。
我建议你熟悉广度优先搜索 ,这通常用于不太大的状态搜索问题。
如果你有一些“知识”,一个启发式,如何搜索迷宫,那么你也可以看看A-Star搜索
在你的情况下,BFS可以做的是:(顺便说一句,很好,并使用适当的建设者,吸气剂和制定者)
public class Cell {
public int x;
public int y;
public Cell parent;
@Override
public boolean equals(Object obj) {
// TODO Override equals so it only incudes x and y coorinates, and not parent
return true;
}
@Override
public int hashCode() {
// TODO Override hash code as well
return 0;
}
}
public Cell seachFor(Cell start, Cell finish) {
Queue<Cell> open = new LinkedList<>();
Set<Cell> closed = new HashSet<>();
open.add(start);
while (!open.isEmpty()) {
Cell current = open.poll();
if (current.equals(finish)) {
return current;
}
closed.add(current);
for (Cell neighbour : getNeighbours(current)) {
if (!closed.contains(neighbour)) {
open.add(neighbour);
}
}
}
return null;
}
private List<Cell> getNeighbours(Cell current) {
/* TODO Set the neighbour's "parent"
* Return valid adjacent neighbour cells
*/
return null;
}
public Deque<Cell> pathfinder(Cell start) {
Deque<Cell> path = new ArrayDeque<>();
path.push(start);
Cell current = start;
while (current.parent != null) {
current = current.parent;
path.push(current);
}
return path;
}
public static void main(String[] args) {
Cell start = maze.getStart();
Cell finish = maze.getFinish();
Deque<Cell> path = pathFinder(searchFor(start, finish))
while (!path.isEmpty()) {
Cell current = path.pop();
maze.moveTo(current);
}
}
请注意,这是一个模拟代码,您需要在它工作之前对其进行优化。
您的代码中没有任何内容可以检测您之前已经存在的位置。 如果你的迷宫包含任何你可以进入一个圆圈或循环回到你之前没有回溯的地方的段落,它将继续无限地沿着同一条路径递减。
你在堆栈上分配的内存耗尽。 也就是说,如果在给定时间超过JVM的资源,则递归速度过快。
你可以增加内存然后杀死这个方法的所有其他迭代,或者更改你的算法。 更改stacksize将是一个糟糕的解决方案但它可能工作,即使您的代码如果有缺陷如何增加eclipse中的堆栈大小我会建议标记您已经访问过哪些区域
迷宫是Cell[][] maze
public class Cell{
boolean visited = false;
Wall left;
Wall right;
Wall up;
Wall bottom;
boolean end;
//Settters and Getters
}
每次点击变量时将访问变量设置为true,然后将代码更改为
public boolean solveMaze(Cell[][] maze, int i, int j){
if (maze[i][j].isEnd()){
System.out.println(maze[i][j].toString());
return true;
}
if (maze[i][j].getButton() == null)&&(maze[i][j].visited==false)){
return solveMaze(maze,i,j+1);
}
if (maze[i][j].getUp() == null){
return solveMaze(maze,i,j-1) ;
}
// ect
我不会实施它,除非你自己有问题,面试问题我觉得它很整洁如果你自己解决它们的提示,你会觉得你可以再次解决类似问题,如果你只是阅读并理解答案你也许能够重申同一问题的答案,但你可能无法用类似的策略想出一个类似问题的新颖答案。
首先,你走下去,直到你到达墙。 然后,一个 - 然后再次下降。 一个向上,一个向下 - 直到java检测到这是非常愚蠢的并且停止了StackOverFlowError;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.