2013-04-26 114 views
0

我有一个程序,读取从文件中的迷宫,并把它变成一个二维数组和导航的方式,虽然使用递归,并放置一个P到处它一直和V每其中两次。在我的算法的某个地方有一个问题,导致迷宫只有Vs.我已经输入了许多println语句来尝试调试该问题。我是Java的新手,我可能错过了一些明显的东西,但我无法找到我生活中的问题。迷宫遍历算法递归

迷宫代码:

public static boolean goNorth(){ 
     boolean success; 
     if(maze[currCol][currRow - 1] == CLEAR){ 
      currRow = currRow - 1; 
      maze[currCol][currRow] = PATH; 
      if (maze[currCol][currRow] == maze[finishCol][finishRow]){ 
      success = true; 
      } else { 
       success = goNorth(); 
       if(!success){ 
       success = goWest(); 
        if(!success){ 
        success = goEast(); 
         if(!success){ 
         maze[currCol][currRow] = VISITED; 
         currRow = currRow + 1; 
         } 
        } 
       } 
      } 
     } else { 
      success = false; 
    } 
    return success; 
} 

public static boolean goWest(){ 
     boolean success; 
     if(maze[currCol - 1][currRow] == CLEAR){ 
      currCol = currCol - 1; 
      maze[currCol][currRow] = PATH; 
      if (maze[currCol][currRow] == FINISH){ 
      success = true; 
      } else { 
      success = goWest(); 
       if(!success){ 
       success = goSouth(); 
        if(!success){ 
        success = goNorth(); 
         if(!success){ 
          maze[currCol][currRow] = VISITED; 
         currCol = currCol + 1; 
         } 
         } 
        } 
       } 
      } else { 
       success = false; 
     } 
     return success; 
    } 

public static boolean goEast(){ 
     boolean success; 
     if(maze[currCol + 1][currRow] == CLEAR){ 
      currCol = currCol + 1; 
      maze[currCol][currRow] = PATH; 
      if (maze[currCol][currRow] == FINISH){ 
      success = true; 
      } else { 
      success = goEast(); 
       if(!success){ 
       success = goNorth(); 
        if(!success){ 
        success = goSouth(); 
         if(!success){ 
          maze[currCol][currRow] = VISITED; 
          currCol = currCol - 1; 
          } 
         } 
        } 
       } 
      } else { 
       success = false; 
     } 
     return success; 
    } 

public static boolean goSouth(){ 
     boolean success; 
     if(maze[currCol][currRow + 1] == CLEAR){ 
      currRow = currRow + 1; 
      maze[currCol][currRow] = PATH; 
      if (maze[currCol][currRow + 1] == FINISH){ 
      success = true; 
      } else { 
      success = goSouth(); 
       if(!success){ 
       success = goEast(); 
        if(!success){ 
        success = goWest(); 
         if(!success){ 
          maze[currCol][currRow] = VISITED; 
          currRow = currRow - 1; 
          } 
         } 
        } 
       } 
      } else { 
       success = false; 
     } 
     return success; 
    } 

所需的输出:

xxxxxxxxxxxxxxxxxxFx 
xVVVVVxPPPPPPPxxxxPx 
xVxxxxxPxxxxxPPPxxPx 
xVxxxxxPxxxxxxxPxxPx 
xVVVVVVPPPPPPxxPxxPx 
xVxxxxxxxxxxPxxPPPPx 
xxxxxxxxxxxxSxxxxxxx 

输出我得到:

xxxxxxxxxxxxxxxxxxVx 
xVVVVVxVVVVVVVxxxxVx 
xVxxxxxVxxxxxVVVxxVx 
xVxxxxxVxxxxxxxVxxVx 
xVVVVVVVVVVVVxxVxxVx 
xVxxxxxxxxxxVxxVVVVx 
xxxxxxxxxxxxSxxxxxxx 
+0

KyleM的调试建议很好。另外,你已经写了4个非常非常相似的函数。有什么方法可以重写代码以减少重复吗?除了让你的代码更易于阅读和维护之外,像这样凝结你的代码常常会使错误更加明显。 – 2013-04-26 03:52:58

回答

1

我已经认真阅读了您的解决方案。你的代码的主要问题是你的递归是凌乱的。举个例子:如果现在是(5,5)(除了墙的所有格子都是CLEAR),并且在你的goNorth方法中。然后,你将进入(4,5)并采用新的goNorth方法,此方法有时候会调用goSouth,但是你会再次进入(5,5)!现在围绕(5,5)的网格不是CLEAR。你不能去任何地方(goNorth和goSouth等将返回false)。

所以,你看到了问题。试着想出一个新的正确的递归来解决这个问题。

3

你调试策略不太好,如果你改变你的调试策略,您将能够解决您的问题。例如,您首要关心的应该是应用程序的状态,第一次选择了不正确的路径(第一次V显示它应该是P的位置)。您需要确定该点并暂停应用程序,查看重要变量的状态。那会告诉你出了什么问题。

我对你的建议是使用Eclipse IDE。在代码的第一个重要部分旁边设置一个断点,只需双击该代码行的左侧(或右键单击并选择断点选项)即可完成。然后,一旦你设置了断点,在Eclipse中启动调试器。您可以很容易地启动调试器,它是工具栏中运行选项旁边的选项。 Eclipse会在它到达断点时自动暂停,并且可以逐步执行代码。 Eclipse会告诉你每个变量的值,你可以使用这个信息来找出错误。

我很抱歉,如果你想解决你的问题,而不是建议如何解决它,但你显然是一个初学者,我不会做你的任何恩惠做你的功课给你。无论如何,祝你好运。

Article调试在Eclipse