【问题标题】:Solve 2D array maze using recursion使用递归解决二维数组迷宫
【发布时间】:2019-03-18 06:00:54
【问题描述】:

我正在编写一个代码来读取带有迷宫的 txt 文件。然后使用递归解决那个迷宫。

我已经多次完全重写了我的代码,但似乎我的代码并没有从起点移动。我不知道为什么会这样。

public static void main(String[] args){
    //Read in maze file...

    //Find starting point
    int startRow = 0;
    int startColumn = 0;
    for(int i = 0; i <maze[0].length; i++)
    {
        if(maze[6][i] == 's')
        {
            startRow = 6;
            startColumn = i;
        }
    }

    if(solve(maze,startRow,startColumn))
    {
        System.out.println("Success");

        for(int r = 0; r < 7; r++)
           {
              for(int c = 0; c < 20; c++)
              {
                 System.out.print(maze[r][c]);
              }
              System.out.println();
           }
    }
    else
    {
        System.out.println("Fail");

        for(int r = 0; r < 7; r++)
           {
              for(int c = 0; c < 20; c++)
              {
                 System.out.print(maze[r][c]);
              }
              System.out.println();
           }
    }
}

public static boolean solve(char[][] maze, int row, int column)
{
      boolean success = false;
      if(valid(maze, row, column))
      {
         maze[row][column] = 'v';  //mark as visited

         if (maze[row][column] == 'f') //check for finish
            success = true;
         else
         {
            success = solve(maze, row - 1, column);  //north
            if(!success)
               success = solve(maze, row, column + 1);  //west
            if(!success)
               success = solve(maze, row, column - 1);  //east
            if(!success)
               success = solve(maze, row + 1, column);  //south
         }
         if(success)  //mark as path
            maze[row][column] = 'p';
      }
      return success;
 }
public static boolean valid(char[][] maze, int row, int column)
{
    boolean a = false;
    if(row >= 0 && row < maze.length && column >= 0 && column < maze[0].length)
         if (maze[row][column] == ' ')
            a = true;
      return a;
}

}

我正在使用 7x20 的文本文件进行测试:

xxxxxxxxxxxxxxxxxxfx
x     x       xxxx x
x xxxxx xxxxx   xx x
x xxxxx xxxxxxx xx x
x            xx xx x
x xxxxxxxxxx xx    x 
xxxxxxxxxxxxsxxxxxxx

'x' = 墙

's' = 开始

'f' = 完成

我的输出:

Fail
xxxxxxxxxxxxxxxxxxfx
x     x       xxxx x
x xxxxx xxxxx   xx x
x xxxxx xxxxxxx xx x
x            xx xx x
x xxxxxxxxxx xx    x
xxxxxxxxxxxxsxxxxxxx

【问题讨论】:

  • 您的调试器将为您提供对此的最佳洞察。使用它在程序运行时单步执行它。我发现了我认为的几个问题:(a) valid() 仅在 (row, column) 处的字符是空格时才返回 true - 因此您不会离开起始点,因为初始字符是 @987654327 @; (b) 您将空间标记为已访问之前您测试它是否完成,所以您永远不会完成。
  • “我正在使用 7x20 文本文件进行测试”请对测试数据进行硬编码,以便更容易提供帮助。见minimal reproducible example

标签: java arrays recursion maze


【解决方案1】:

您的搜索永远不会开始,因为您的 valid 方法将起点 s 报告为无效。

快速解决方法是改变:

if (maze[row][column] == ' ')
  a = true;

if (maze[row][column] == ' ' || maze[row][column] == 's')
  a = true;

另一个问题是您在检查之前覆盖了完成单元格:

maze[row][column] = 'v';  //mark as visited

if (maze[row][column] == 'f') //check for finish
  success = true;

您需要重新构建求解方法,使其看起来像这样:

public static boolean solve(char[][] maze, int row, int column)
{
      boolean success = false;

      if (maze[row][column] == 'f') //check for finish
        success = true;
      else if(valid(maze, row, column))
      {
         maze[row][column] = 'v';  //mark as visited

          success = solve(maze, row - 1, column);  //north
          if(!success)
             success = solve(maze, row, column + 1);  //west
          if(!success)
             success = solve(maze, row, column - 1);  //east
          if(!success)
             success = solve(maze, row + 1, column);  //south

         if(success)  //mark as path
            maze[row][column] = 'p';
      }
      return success;
}

通过这些更改,您的代码运行良好:

Success
xxxxxxxxxxxxxxxxxxfx
x     xpppppppxxxxpx
x xxxxxpxxxxxpppxxpx
x xxxxxpxxxxxxxpxxpx
x      ppppppxxpxxpx
x xxxxxxxxxxpxxppppx
xxxxxxxxxxxxpxxxxxxx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多