在Depth First Search中获取StackOverflowError

时间:2015-04-24 18:40:45

标签: java stack stack-overflow depth-first-search

此程序尝试使用我创建的Stack类和深度优先搜索算法来解决迷宫。我在location = stack.peek();findPath( maze,location,stack );时收到错误。我猜我必须改变递归调用来修复错误。我不知道如何实现这一点。

我的LinkedStack类

import java.awt.Point;
public class LinkedStack {
private Node top;

public LinkedStack() {
    top = null;
}
public boolean isEmpty() {
    return top == null;
}
public void push( Point p ) {
    top = new Node (p, top);
}
public Point pop() {
    Point retVal = new Point(0,0);
    if( isEmpty() ){
        System.out.println("Nothing to remove");
    }else{
        retVal = top.getValue();
        top = top.getNext();
    }
    return retVal;
}
public Point peek() {
    Point retVal = new Point(0,0);
    if( isEmpty() ){
        System.out.println("Stack is Empty");
    }else{
        retVal = top.getValue();
    }
    return retVal;
}
public String toString(){
    String s = "";
    Node n = top;
    while( n != null ){
        s = s + n.getValue() + " ";
        n = n.getNext();
    }
    return s;
}
}

发生StackOverFlowError的主类

//Should mark location of path taken with '.'
//Should check neighboring spots of location (up,right,down,left)
//Should check if valid locations
public static boolean findPath( char [][] maze, Point location, LinkedStack stack ){
    boolean hasSolution = false;
    stack.push(location);

    do{
        maze[location.x][location.y] = '.';

        if( location.y > 0 ){
            if( maze[location.x][location.y - 1] == ' '){
                stack.push(new Point( location.x, location.y - 1));
                maze[location.x][location.y - 1] = '.';
            }
        }
        if( location.y < maze[location.x].length ){
            if( maze[location.x][location.y + 1] == ' '){
                stack.push(new Point( location.x, location.y + 1));
                maze[location.x][location.y + 1] = '.';
            }
        }
        if( location.x < maze.length ){
            if( maze[location.x + 1][location.y] == ' '){
                stack.push(new Point( location.x + 1, location.y ));
                maze[location.x + 1][location.y] = '.';
            }
        }
        if( location.x > 0 ){
            if( maze[location.x - 1][location.y] == ' '){
                stack.push(new Point( location.x - 1, location.y ));
                maze[location.x - 1][location.y] = '.';
            }
        }
        if( maze[location.x][location.y] == 'f' ){
             hasSolution = true;
        }

        location = stack.peek();
        stack.pop();
        findPath( maze,location,stack );
    }while( !location.equals('f') && !stack.isEmpty() );
    return hasSolution;
}

1 个答案:

答案 0 :(得分:0)

请更改以下内容:

       ...
       location = stack.peek();
       stack.pop();
       findPath( maze,location,stack );
    } while( !location.equals('f') && !stack.isEmpty() );
    ...

为:

        ...
        location = stack.peek();
        stack.pop();
        //findPath(maze, location, stack);
    } while (!(hasSolution || stack.isEmpty()));
    ...

内在findPath()无限地递归! (1.)并且正如注释正确陈述,您尝试将Point(x,y)与f(字符)进行比较,while循环永远不会终止。 (2)。

编辑:这仍然无法解决您的问题,因为:hasSolution永远不会是true(没有丝毫机会!) - 为什么? - 因为您(始终)设置maze[x][y] = '.';(从未设置'f'),并尝试设置hasSolution = maze[x][y] == 'f'(这将总是收益false)< / p>

请重新考虑您的算法和方法!迷宫应包含什么?什么时候应该改变?