使用递归求解2D阵列迷宫

时间:2018-10-13 02:52:09

标签: java arrays recursion maze

我正在编写代码,用迷宫读取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

1 个答案:

答案 0 :(得分: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;

您需要重组您的solve方法,使其看起来像这样:

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