繁体   English   中英

穿越迷宫时出现堆栈溢出错误

[英]Stack Overflow error when traversing through a maze

我正在尝试递归遍历迷宫,但遇到堆栈溢出错误,我理解问题,但无法修复。 我是否需要创建一个单独的数组来保存数组中已访问过的所有值,或者是否有其他方法可以使用更少的代码行更高效?

任何建议将不胜感激。

这是输入;

5 6       // Row,Col
1 1       // Start Pos
3 4       // End Pos 
1 1 1 1 1
1 0 0 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 1 1 1 1

当前代码:

public class Solve {

    private static int [][] MazeArray;
    private static int Rows;
    private static int Cols;
    private static Point end = new Point();
    private static Point start = new Point();

    public static void ReadFileMakeMaze() {

        Scanner in = new Scanner(System.in);
        System.out.print("Select File: "); // Choose file
        String fileName = in.nextLine();
        fileName = fileName.trim();

        String Buffer = "";
        String[] Buffer2;
        String[] MazeBuffer;
        int Counter = 0;

        try {

            // Read input file
            BufferedReader ReadFileContents = new BufferedReader(new FileReader(fileName+".txt"));
            Buffer = ReadFileContents.readLine();
            MazeBuffer = Buffer.split(" ");

            // Creating MazeArray according to rows and columns from input file.
            Rows = Integer.parseInt(MazeBuffer[0]); 
            Cols = Integer.parseInt(MazeBuffer[1]);
            MazeArray = new int[Rows][Cols];

            // Retrieving start locations and adding them to an X and Y coordinate.
            String[] StartPoints = ReadFileContents.readLine().split(" ");
            start.x = Integer.parseInt(StartPoints[0]);
            start.y = Integer.parseInt(StartPoints[1]);

            // Retrieving end locations and adding them to an X and Y coordinate.
            String[] EndPoints = ReadFileContents.readLine().split(" ");
            end.x = Integer.parseInt(EndPoints[0]);
            end.y = Integer.parseInt(EndPoints[1]);

            while(ReadFileContents.ready()) {
                Buffer = ReadFileContents.readLine();
                Buffer2 = Buffer.split(" ");

                for(int i = 0; i < Buffer2.length; i++) {
                    MazeArray[Counter][i] = Integer.parseInt(Buffer2[i]); // Adding file Maze to MazeArray.
                }
                Counter ++;
                }
            }

            catch(Exception e){
                System.out.println(e); // No files found. 
            }

        System.out.println(SolveMaze(start.x, start.y));
    }


    public static boolean SolveMaze(int x,int y) {

        Print(); // Printing the maze

        if(ReachedEnd(x,y)) {
            MazeArray[x][y] = 5; // 5 represents the end
            System.out.println(Arrays.deepToString(MazeArray));
            return true;

        }else if(MazeArray[x][y] == 1 || MazeArray[x][y] == 8){
            return false;

        }else {

            MazeArray[x][y] = 8; // Marking the path with 8's           
            start.x = x;
            start.y = y;

            // Checking all directions
            if(MazeArray[x][y - 1] == 0 ) {
                System.out.println("Left");
                SolveMaze(x, y - 1);

            }else if(MazeArray[x + 1][y] == 0) {
                System.out.println("Down");
                SolveMaze(x + 1, y);

            }else if(MazeArray[x - 1][y] == 0 ) {
                System.out.println("Up");
                SolveMaze(x - 1, y);

            }else if(MazeArray[x][y + 1] == 0 ) {
                System.out.println("Right");
                SolveMaze(x, y + 1);

            }else {
                System.out.println("Debug");
                MazeArray[x][y] = 0;
                start.x = x;
                start.y = y;
            }
        }
        return false;
    }

    public static boolean DeadEnd(int x, int y) {
        return true; // Solution needed
    }

    public static boolean ReachedEnd(int x, int y) {

        if(x == end.x && y == end.y) { // Check if the end has been reached.  
            return true;
        }else {
            return false;
        }
    }

    public static void Print() {
        System.out.println(Arrays.deepToString(MazeArray));
    }

    public static void main(String[] args) {
        ReadFileMakeMaze();
    }
}

首先,就像你在问题中提到的那样,在 SolveMaze 之外创建一个静态集合来保存“访问过的”节点列表肯定会有所帮助。 如果之前已经访问过该节点,则无需再次检查。

其次,我认为上面的代码中存在一些错误。 该表未正确生成到 MazeArray[][],在“//根据输入文件中的行和列创建 MazeArray”下方交换行和列。

代码也没有“找到”路径,数组被定义为 int[] array = new int[5] 但是要访问它你必须使用 array[0] --> array[4] ,当我修复迷宫是你从元素 1,1 开始,它是从左上角开始的“0”一行和一列。 然后根据 if else 语句的顺序向下遍历列表。 访问每个节点后,您将节点值变为 8。经过 4 次迭代后,代码查看第 2 行的底部 0,也就是您上方的 [4][1] 现在是 8,因为 0 是您唯一的元素可以移动到,节点向左、向右、向下看,看到1,然后转身向上看,看到8。这样就完成了。

第三,不要把检查上、下、左、右一组“if else”,只是选择“if”。 这样代码将遍历所有路径。 然后你可以保留你的 8,它实际上不会重新运行同一个节点两次,从而不需要 'visited' 数组。 (虽然作为一般规则,不建议遍历时改变状态)

第四,我无法从上面的代码中得到堆栈溢出错误,因此无法回答实际问题。

附带说明 - 使用测试和诸如此类的事情变得微不足道,从小处着手并构建它:) 如果不确定,请检查 junit。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM