程序编译但运行时出错

时间:2013-04-23 17:54:38

标签: java maze

尝试运行此代码时遇到错误,但在编译时却没有。在每个方向方法的最后,我按照应该进入的顺序调用下一个方法。我要做的是创建一个程序,从文件中读取迷宫并解决它。在解决问题的同时,我希望程序为它所采用的路径放置一个P,为已经访问过的地方放置一个V.该程序只允许向左前进,然后返回每个方向。当你走向不同的方向时,你将不得不遵循这些规则,除了你面对的方式现在是新的前锋。

迷宫代码:

    public static boolean goNorth(){
        boolean success;
        if(maze[startCol][startRow] == maze[finishCol][finishRow]){
        return true;
        }
            if(maze[startCol][startRow - 1] == CLEAR){
                maze[startCol][startRow - 1] = PATH;
                startRow = startRow - 1;
                goNorth();
                if(maze[startCol][startRow - 1] == PATH){
                    maze[startCol][startRow - 1] = VISITED;
                    startRow = startRow - 1;
                    goNorth();
                    if (maze[startCol][startRow - 1] == VISITED){
                        goSouth();
                    }   
                }
            }
        goWest();
        return true;    
    }


    public static boolean goEast(){
        boolean success;
        if(maze[startCol][startRow] == maze[finishCol][finishRow]){
        return true;
        }
            if(maze[startCol + 1][startRow] == CLEAR){
                maze[startCol + 1][startRow] = PATH;
                startCol = startCol + 1;
                goEast();
                if(maze[startCol + 1][startRow] == PATH){
                    maze[startCol + 1][startRow] = VISITED;
                    startCol = startCol + 1;
                    goEast();
                    if(maze[startCol + 1][startRow] == VISITED){
                        goWest();
                    }
                }
            }
        goNorth();
        return false;   
    }


    public static boolean goSouth(){
        boolean success;
        if(maze[startCol][startRow] == maze[finishCol][finishRow]){
            return true;
            }

            if(maze[startCol][startRow + 1] == CLEAR){
                maze[startCol][startRow + 1] = PATH;
                startRow = startRow + 1;
                goSouth();
                if(maze[startCol][startRow + 1] == PATH){
                    maze[startCol][startRow + 1] = VISITED;
                    startRow = startRow + 1;
                    goSouth();
                    if (maze[startCol][startRow + 1] == VISITED){
                        goNorth();
                    }   
                }   
            }
        goEast();
        return false;   
        }

    public static boolean goWest(){
        boolean success;
        if(maze[startCol][startRow] == maze[finishCol][finishRow]){
        return true;
        }
            if(maze[startCol - 1][startRow] == CLEAR){
                maze[startCol - 1][startRow] = PATH;
                startCol = startCol - 1;
                goWest();
                if(maze[startCol - 1][startRow] == PATH){
                    maze[startCol - 1][startRow] = VISITED;
                    startCol = startCol - 1;
                    goWest();
                    if(maze[startCol - 1][startRow] == VISITED){
                        goEast();
                    }
                }   
            }
        goSouth();
        return false;
    }
}

Maze.txt:

20 7
0 18
6 12
xxxxxxxxxxxxxxxxxx x
x     x       xxxx x
x xxxxx xxxxx   xx x
x xxxxx xxxxxxx xx x
x            xx xx x
x xxxxxxxxxx xx    x 
xxxxxxxxxxxx xxxxxxx

错误:

http://s22.postimg.org/etar0eq1t/Code.png

1 个答案:

答案 0 :(得分:4)

知道了。您的代码首先调用North,如果不清楚调用West,如果不清楚调用South,则调用East。不幸的是,你的东部呼叫不清楚,然后再次呼叫北,这将循环上面,吹你的筹码。你的递归需要一个更好的终止案例。

此外,此处没有“地图边缘”检测,因此在初始条件不佳的情况下可能会退出迷宫并丢弃内存。想象你的起始位置在迷宫的北边。你的第一个电话就是去北方,这会读取你的阵列并将事情搞砸。你需要在洪水填充中检查地图大小,走迷宫呼叫。

相关问题