2016-09-26 65 views
0

需要在正确的方向上进行类别分配。我已阅读其他职位,提到创建一个变量/方法来存储路径,但不知道如何得到它... 编辑9/28/16 能够达到迷宫的终点,但还没有想出 如何打印只采取的路径;我真的需要陷入递归迷宫

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); 
    } 

} 

==== 所以我不知道如何实现一个方法来跟踪路径行进,任何指针将不胜感激!

+3

一些关于你想达到什么的解释将不胜感激。没有仔细观察你的代码,我想你想解决迷宫?如果是这样,你是否必须使用特定的算法或解决它?此外,你能否描述你的实现和你遇到的问题更详细? – Turing85

+0

欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在您发布代码并准确描述问题之前,我们无法有效帮助您。 – Prune

+0

请参阅本堆栈溢出问题清单,以帮助您提出一个问题,以帮助您朝正确的方向发展。 http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist –

回答

1

简单的方法是等到找到解决方案。然后简单地记录成功的动作,当你爬回呼叫树的那个分支时。每个获胜的调用都会将其移到返回值的前面,并将其传递回堆栈。这将是类似于

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

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

其次,您需要实现逻辑以在找到一个解决方案后放弃其他搜索。设置完成并没有多大帮助;这是一个本地变量,并且您需要与调用程序通信,告诉您失败或成功。

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