Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 导致堆栈溢出错误的搜索算法_Java_Stack_Depth First Search - Fatal编程技术网

Java 导致堆栈溢出错误的搜索算法

Java 导致堆栈溢出错误的搜索算法,java,stack,depth-first-search,Java,Stack,Depth First Search,我正在使用所谓的深度优先搜索来解决一个迷宫,使用我自己为任务创建的堆栈类。我认为我出现此错误的原因是因为我的程序从未满足finishSpot条件。我使用了调试器,它看起来像是无限地卡在finishSpot之前的点上。我的逻辑似乎基本正确,除非我的迷宫解算器试图完成迷宫,所以我只需要帮助满足导致程序崩溃的最后一个条件 这是一个示例迷宫,其中“s”表示开始,“f”表示结束,“*”表示墙 ***** *s* * * * * * f* ***** 这是我的Main,它使用我的LinkedStack类

我正在使用所谓的深度优先搜索来解决一个迷宫,使用我自己为任务创建的堆栈类。我认为我出现此错误的原因是因为我的程序从未满足
finishSpot
条件。我使用了调试器,它看起来像是无限地卡在
finishSpot
之前的点上。我的逻辑似乎基本正确,除非我的迷宫解算器试图完成迷宫,所以我只需要帮助满足导致程序崩溃的最后一个条件

这是一个示例迷宫,其中“s”表示开始,“f”表示结束,“*”表示墙

*****
*s* *
* * *
*  f*
*****
这是我的Main,它使用我的LinkedStack类,如果需要,我可以发布它

//Creates a Stack and determines the start and endpoints.
public static void solveDFS( char [][] maze ){
    LinkedStack stack = new LinkedStack();
    Point currentSpot = findPoint( maze,'s' );
    Point finishSpot = findPoint( maze, 'f' );
    findPath( maze,currentSpot, finishSpot,stack );  
}
//Finds a point by searching for a char.
private static Point findPoint( char [][] maze, char c ) {
    for ( int i = 0; i < maze.length; i++ ) {
        for ( int j = 0; j < maze[i].length; j++ ) {
            if ( maze[i][j] == c ) {
                return new Point(i, j);
            }
        }
    }
    return null;
}
//Search algorithm looks for all neighbor locations
private static boolean findPath( char [][] maze, Point currentSpot, Point finishSpot, LinkedStack stack ){
    boolean hasSolution = false;
    stack.push(currentSpot);

    while( currentSpot != finishSpot && !stack.isEmpty() ){
        // Checks Right
        if( currentSpot.x < maze.length ){
            if( maze[currentSpot.x + 1][currentSpot.y] == ' '){
                stack.push(new Point( currentSpot.x + 1, currentSpot.y ));
            }
        }
        // Checks Left
        if( currentSpot.x > 0 ){
            if( maze[currentSpot.x - 1][currentSpot.y] == ' '){
                stack.push(new Point( currentSpot.x - 1, currentSpot.y ));
            }
        }
        // Checks Up
        if( currentSpot.y > 0 ){
            if( maze[currentSpot.x][currentSpot.y - 1] == ' ' ){
                stack.push(new Point( currentSpot.x, currentSpot.y - 1));
            }
        }
        // Checks Down
        if( currentSpot.y < maze[currentSpot.x].length ){
            if( maze[currentSpot.x][currentSpot.y + 1] == ' '){
                stack.push(new Point( currentSpot.x, currentSpot.y + 1));
            }
        }
        // Checks Finish (My program never meets this condition help!)
        if( currentSpot == finishSpot ){
            printMaze(maze);
            hasSolution = true;
        }
        currentSpot = stack.pop();
        findPath( maze, currentSpot, finishSpot, stack );
    }   
    return hasSolution;
}
//创建堆栈并确定起点和终点。
公共静态void solveDFS(字符[][]迷宫){
LinkedStack堆栈=新LinkedStack();
点currentSpot=findPoint(迷宫,'s');
点finishSpot=findPoint(迷宫,'f');
findPath(迷宫、currentSpot、finishSpot、堆栈);
}
//通过搜索字符来查找点。
专用静态点findPoint(字符[][]迷宫,字符c){
对于(int i=0;i0){
如果(迷宫[currentSpot.x-1][currentSpot.y]=''){
堆栈推送(新点(currentSpot.x-1,currentSpot.y));
}
}
//检查
如果(currentSpot.y>0){
如果(迷宫[currentSpot.x][currentSpot.y-1]=''){
堆栈推送(新点(currentSpot.x,currentSpot.y-1));
}
}
//检查
if(currentSpot.y
以下条件在您的代码中永远不会同时为真

while( currentSpot != finishSpot && !stack.isEmpty() ){
    ...
    if( currentSpot == finishSpot ){

我认为你不能像那样比较
变量。 尝试重写方法
equals


如果(currentSpot==finishSpot)

读取。。。我认为你不应该在这里使用
==
。使用
if(currentSpot.equals(finishSpot))
替代
Point
类中的
equals
hashCode
。为什么要使用堆栈而不是递归?@mangusta我需要这样做是为了分配。