陷入递归迷宫

时间:2016-09-26 21:31:02

标签: java recursion maze

需要按正确的方向推进课堂作业。我读过其他帖子,提到创建一个变量/方法来存储行进的路径,但不知道如何解决它...     16/9/28编辑     能够到达迷宫的终点,但仍然没有想到     如何只打印所采取的路径;我真的需要

import java.io.*;
import java.util.*;

public class Maze
{
    private static int rows, cols, startRow, startCol, nextRow, nextCol;
    private static int endRow = 3;
    private static int endCol = 34;
    private static char[][] mazeBoard;
    //private static char start = 'S';
    private static char end = 'E';
    //private boolean finish = false;
    private char[][] explored = new char[rows][cols];

    //construct the maze board
    public Maze() throws FileNotFoundException
    {
        Scanner in = new Scanner(new File("maze.txt"));
        rows = in.nextInt();
        cols = in.nextInt();
        startRow = in.nextInt();
        startCol = in.nextInt();

        //fill out the mazeBoard
        mazeBoard = new char[rows][cols];
        int i = 0;
        while (in.hasNextLine())
        {
            String inLine = in.nextLine();
            if (inLine.isEmpty())
            {
            continue;   
            }
            for (int j = 0;j < cols; j++)
            {   
            char nextChar = inLine.charAt(j);
            mazeBoard[i][j] = nextChar;
            System.out.print(nextChar);     
            }
            System.out.println();
            i++;        
        }
        in.close();
    }
    //updated the move method from void to boolean
    public boolean move(int row, int col, int prevRow, int prevCol)
{
    boolean finish = false; 
    prevRow = row;
    prevCol = col;
    //show location
    System.out.println("row: " + row + " col: " + col);

    //base case1 to check for out of bounds and not the previous position
    if (row < 0 || col < 0 || row >= rows || col >= cols || row != prevRow || col != prevCol)
        {   return false;   }   
    //base case2 to see if reached exit/end point
    if (row == endRow && col == endCol)
    {
        System.out.println("Found the exit!");
        return true;
    }   
    //base case3 to check for wall
    if (mazeBoard[row][col] == '+' || mazeBoard[row][col] == '*')
        {   return false;   }
    mazeBoard[row][col] = '*';  
        //try to move down
        if (move(row + 1, col, prevRow, prevCol))
            {   return true;    }
        //try to move right
        if (move(row, col + 1, prevRow, prevCol))
            {   return true;    }
        //try to move up
        if (move(row - 1, col, prevRow, prevCol))
            {   return true;    }
        //try to move left
        if (move(row, col - 1, prevRow, prevCol))
            {   return true;    }   
            row = prevRow;
            col = prevCol;
    return false;   
}

    public static void main(String[] args) throws FileNotFoundException
    {
        Maze maze = new Maze();
        maze.move(startRow, startCol);
    }

}

==== 所以我不确定如何实现一种跟踪行进路径的方法,任何指针都会非常感激!

1 个答案:

答案 0 :(得分:1)

简单的方法是等到找到解决方案。然后,只需在爬行调用树的分支时记录成功的移动。每个获胜的调用都会将其移动到返回值的前面并将其传递回堆栈。这就像是

result = move(rowM + 1, colM);
if result != ""
    return "D" + result;   // "D" for a move right
else {
    // Try a move right ...

你有几件事需要解决。最重要的是,你必须阻止你已采取的步骤。现在,当你的搜索遇到死胡同时,它会不断重复最后一步,并在无限递归中回溯。

其次,在您找到一个解决方案后,您需要实施逻辑以中止其他搜索。设置完成并不会有多大帮助;这是一个本地变量,你需要与你失败或成功的调用程序进行通信。

这足以让你进入下一步吗?