递归迷宫求解器问题Java



我现在已经让它停止无限重复,但是它只是一遍又一遍地尝试相同的错误路径。有人知道一种让它尝试不同路径的方法吗?

数字的关键:0是开放的1是墙2是路径的一部分3是迷宫的末端

    public class Maze{
  public static void main(String[] args){
    int[][] maze = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
      {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1},
      {1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,0,1},
      {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,1},
      {1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,1,1,0,1},
      {1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1},
      {1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,0,1,0,1},
      {1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1},
      {1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,1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1},
      {1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,1,1,0,1},
      {1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1},
      {1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1},
      {1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1},
      {1,0,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0,1,0,1,0,1},
      {1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1},
      {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1}};
    boolean[][] posCheck = new boolean[maze.length][maze[0].length];
    int r = 0;
    int c = 0;
    for(int row = 0; row < maze.length; row++){
      for(int col = 0; col < maze[row].length; col++){
        if(maze[row][col]==0){
          r = row;
          c = col;
        }
      }
    }
    maze[r][c] = 3;
    mazeSolver(1, 0, maze, posCheck);
  }
  public static boolean mazeSolver(int r, int c, int[][]maze, boolean[][] posCheck){
    posCheck[r][c] = true;
    maze[r][c] = 2;
    if(maze[r][c] == 3){
      print(maze);
      return true;
    }
    if((c+1 < maze.length) && maze[r][c+1]==0 && !posCheck[r][c+1] && (mazeSolver(r, c + 1, maze, posCheck))){
      maze[r][c] = 2;
      return true;
    }
    if((r-1 >= 0) && maze[r-1][c]==0 && !posCheck[r-1][c] && (mazeSolver(r - 1, c, maze, posCheck))){
      maze[r][c] = 2;
      return true;
    }
    if((c-1 >= 0) && maze[r][c-1]==0 && !posCheck[r][c-1] && (mazeSolver(r, c - 1, maze, posCheck))){
      maze[r][c] = 2;
      return true;
    }
    if((r+1 < maze.length) && maze[r+1][c]==0 && !posCheck[r+1][c] && (mazeSolver(r + 1, c, maze, posCheck))){
      maze[r][c] = 2;
      return true;
    }
    print(maze);
    return false;
  }
  public static void print(int[][] maze){
    for(int row = 0; row<maze.length; row++){
      for(int col = 0; col<maze[row].length; col++)
        System.out.print(maze[row][col]);
      System.out.println();
    }
  }
}

,假设您有:初始状态

SW00WW
00W0WW
W000WW
W0WWWW
00000E

W-墙0-不言语的路径S-起点E-终点(x-行走点)

说明:

我们做什么?当我们到达叶子时,我们迭代0并用X标记它们。如果您从另一点开始,则只有在您确定不需要再次返回的情况下,就不会将其标记为x。

www00W     (see 'P' as an '0') we must go from 'S' to 'E' when we reach 'P' we have 2 moves from that point 
S00Pww      when iterating. As conclusion you let it be stil '0', and when we meet a first node 
www00E      that wont need another visit mark it as X.

示例:

SW00WW
x0W0WW
W000WW
W0WWWW
00000E
SW00WW
xxW0WW
W000WW
W0WWWW
00000E
SW00WW
xxW0WW
W000WW
W0WWWW
00000E
SW00WW
x0W0WW
W000WW
W0WWWW
00000E
SW00WW
xxW0WW
W0x0WW
W0WWWW
00000E
SW00WW
xxW0WW
W0xxWW
W0WWWW
00000E
SWxxWW   (made 3 steps on a first ipotetical choise)
xxWxWW
W0xxWW
W0WWWW
00000E
    SWxxWW   (made 4 steps on last row to the end)
    xxWxWW
    WxxxWW
    WxWWWW
    0xxxxE

希望它会有所帮助,对不起,长期以来,我试图清楚地表明。

ps:替代深度第一次搜索

如果将所有位置有效性测试放在一个地方:

public static boolean mazeSolver(int r, int c, int[][]maze){
  if( ! isPositionValid(r, c, maze))
    return false;       // tried to flow outside the maze
  if(maze[r][c] == 3){  // is it a destination point?
    print(maze);        // solved
    return true;
  }
  if( maze[r][c] != 0)  // a wall, a path or already checked?
    return false;
  maze[r][c] = 2;       // mark position as a part of the path
  if( mazeSolver(r, c + 1, maze)))  // try to extend the path and
    return true;                    // return if solution found
  if( mazeSolver(r, c - 1, maze)))
    return true;
  if( mazeSolver(r + 1, c, maze)))
    return true;
  if( mazeSolver(r - 1, c, maze)))
    return true;
  maze[r][c] = 4;     // dead-end - mark the position 'checked'
  return false;
}

public static boolean isPositionValid(int r, int c, int[][]maze){
  return r >= 0 && c >= 0 && r < maze.size && c < maze[r].size;
}

最新更新