当前位置: 首页 > 面试题库 >

Java递归迷宫求解器问题

林冥夜
2023-03-14
问题内容

我正在尝试使用递归编写一个迷宫求解器,似乎它尝试每个方向一次,然后停止,我不知道为什么。如果您发现问题,请告诉我。钥匙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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0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,1,0,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, 0, maze, posCheck);
  }

  public static boolean mazeSolver(int r, int c, int d, int[][]maze, boolean[][] posCheck){
    maze[r][c] = 2;

    if(maze[r][c] == 3){
      print(maze);
      return true;
    }

    if((c+1 < maze.length) && maze[r][c+1]==0 && d != 1 && !posCheck[r][c+1]){
      if(d != 3)
        posCheck[r][c+1] = true;
      if(mazeSolver(r, c + 1, 3, maze, posCheck)){
        maze[r][c] = 2;
        return true;
      }
    }

    if((r-1 >= 0) && maze[r-1][c]==0 && !posCheck[r-1][c] && d != 2){
      if(d != 4)
        posCheck[r-1][c] = true;
      if(mazeSolver(r - 1, c, 4, maze, posCheck)){
        maze[r][c] = 2;
        return true;
      }
    }

    if((c-1 >= 0) && maze[r][c-1]==0 && !posCheck[r][c-1] && d != 3){
      if(d != 1)
        posCheck[r][c-1] = true;
      if(mazeSolver(r, c - 1, 1, maze, posCheck)){
        maze[r][c] = 2;
        return true;
      }
    }

    if((r+1 < maze.length) && maze[r+1][c]==0 && !posCheck[r+1][c] && d != 4){
      if(d != 2)
        posCheck[r+1][c] = true;
      if(mazeSolver(r + 1, c, 4, 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();
    }
  }
}

问题答案:

在过去五个小时中,您已经问过有关此迷宫递归难题的四个问题,这证明它有多复杂。这整个概念的1/0迷宫电网已吸引了我,我想出了一个类,使它成为一个 整体
变得简单许多。如果需要进行递归,那么它将对您没有用,但是如果您可以使用它,那么它将消除很多复杂性。

有两个类,一个枚举。

首先,枚举定义您要在网格中移动的方向,并基于其移动一次确定新索引。

enum Direction {
   UP(-1, 0),
   DOWN(1, 0),
   LEFT(0, -1),
   RIGHT(0, 1);

   private final int rowSteps;
   private final int colSteps;
   private Direction(int rowSteps, int colSteps)  {
      this.rowSteps = rowSteps;
      this.colSteps = colSteps;
   }
   public int getNewRowIdx(int currentRowIdx)  {
      return  (currentRowIdx + getRowSteps());
   }
   public int getNewColIdx(int currentColIdx)  {
      return  (currentColIdx + getColSteps());
   }
   public int getRowSteps()  {
      return  rowSteps;
   }
   public int getColSteps()  {
      return  colSteps;
   }
};

主类称为MazePosition(下)。首先,通过其int[][]构造函数将迷宫网格双数组设置到其中,并静态存储该实例:

private static final MazePosition MAZE_HOLDER = new MazePosition(MAZE_GRID);

(可以将这一步骤设计得更好,但可以。)

设置迷宫网格(每次执行时,这是一次性的事情)之后,然后使用x / y构造函数声明初始位置:

MazePosition pos = new MazePosition(0, 0);

然后,根据需要移动:

pos = pos.getNeighbor(Direction.RIGHT);
pos = pos.getNeighbor(Direction.RIGHT);
pos = pos.getNeighbor(Direction.DOWN);
...

每个位置的值由pos.getValue()pos.isPath()-我认为1是“墙”并且0是“路径”
来检索。(顺便说一句:巨大的2d数组实际上应该包含一个bit booleans,而不是4个字节ints,但是使用s来 查看
数组的代码是有意义int的,而不是使用布尔值。请注意,至少它应该是改为bytes。)

因此,关于移动,如果您尝试在没有邻居时找到邻居,例如在左边缘向左移动,IllegalStateException则会抛出an
。使用is*Edge()功能可以避免这种情况。

MazePosition类还具有一个称为的便捷调试函数getNineByNine(),该函数返回9x9的数组值网格(作为字符串),其中中间项是当前位置。

   import  java.util.Arrays;
   import  java.util.Objects;
class MazePosition  {
//state
   private static int[][] MAZE_GRID;
   private final int rowIdx;
   private final int colIdx;
//internal
   private final int rowIdxMinus1;
   private final int colIdxMinus1;
   public MazePosition(int[][] MAZE_GRID)  {
      if(this.MAZE_GRID != null)  {
         throw  new IllegalStateException("Maze double-array already set. Use x/y constructor.");
      }
      MazePosition.MAZE_GRID = MAZE_GRID;

      //TODO: Crash if null or empty, or sub-arrays null or empty, or unequal lengths, or contain anything but 0 or -1.

      rowIdx = -1;
      colIdx = -1;
      rowIdxMinus1 = -1;
      colIdxMinus1 = -1;
   }
   public MazePosition(int rowIdx, int colIdx)  {
      if(MazePosition.MAZE_GRID == null)  {
         throw  new IllegalStateException("Must set maze double-array with: new MazePosition(int[][]).");
      }

      if(rowIdx < 0  ||  rowIdx >= MazePosition.getRowCount())  {
         throw  new IllegalArgumentException("rowIdx (" + rowIdx + ") is invalid.");
      }
      if(colIdx < 0  ||  colIdx >= MazePosition.getColumnCount())  {
         throw  new IllegalArgumentException("colIdx (" + colIdx + ") is invalid.");
      }

      this.rowIdx = rowIdx;
      this.colIdx = colIdx;
      rowIdxMinus1 = (rowIdx - 1);
      colIdxMinus1 = (colIdx - 1);
   }

   public boolean isPath()  {  
      return  (getValue() == 0);  //1???
   }
   public int getValue()  {
      return  MazePosition.MAZE_GRID[getRowIdx()][getColumnIdx()];
   }
   public int getRowIdx()  {
      return  rowIdx;
   }
   public int getColumnIdx()  {
      return  colIdx;
   }
   public MazePosition getNeighbor(Direction dir)  {
      Objects.requireNonNull(dir, "dir");
      return  (new MazePosition(
         dir.getNewRowIdx(getRowIdx()), 
         dir.getNewColIdx(getColumnIdx())));
   }
   public MazePosition getNeighborNullIfEdge(Direction dir)  {
      if(isEdgeForDirection(dir))  {
         return  null;
      }
      return  getNeighbor(dir);
   }
   public int getNeighborValueNeg1IfEdge(Direction dir)  {
      MazePosition pos = getNeighborNullIfEdge(dir);
      return  ((pos == null) ? -1 : pos.getValue());
   }
   public static final int getRowCount()  {
      return  MAZE_GRID.length;
   }
   public static final int getColumnCount()  {
      return  MAZE_GRID[0].length;
   }
   public boolean isEdgeForDirection(Direction dir)  {
      Objects.requireNonNull(dir);
      switch(dir)  {
         case UP:    return isTopEdge(); 
         case DOWN:  return isBottomEdge(); 
         case LEFT:  return isLeftEdge();
         case RIGHT: return isRightEdge(); 
      }
      throw  new IllegalStateException(toString() + ", dir=" + dir);
   }
   public boolean isLeftEdge()  {
      return  (getColumnIdx() == 0);
   }
   public boolean isTopEdge()  {
      return  (getRowIdx() == 0);
   }
   public boolean isBottomEdge()  {
      return  (getRowIdx() == rowIdxMinus1);
   }
   public boolean isRightEdge()  {
      return  (getColumnIdx() == colIdxMinus1);
   }
   public String toString()  {
      return  "[" + getRowIdx() + "," + getColumnIdx() + "]=" + getValue();
   }
   public String getNineByNine()  {
      int[][] nineByNine = new int[3][3];

      //Middle row
         nineByNine[1][1] = getValue();
         nineByNine[1][0] = getNeighborValueNeg1IfEdge(Direction.LEFT);
         nineByNine[1][2] = getNeighborValueNeg1IfEdge(Direction.RIGHT);

      //Top
         MazePosition posUp = getNeighborNullIfEdge(Direction.UP);
         if(posUp != null)  {
            nineByNine[0][0] = posUp.getNeighborValueNeg1IfEdge(Direction.LEFT);
            nineByNine[0][1] = posUp.getValue();
            nineByNine[0][2] = posUp.getNeighborValueNeg1IfEdge(Direction.RIGHT);
         }

      //Bottom
         MazePosition posDown = getNeighborNullIfEdge(Direction.DOWN);
         if(posDown != null)  {
            nineByNine[2][0] = posDown.getNeighborValueNeg1IfEdge(Direction.LEFT);
            nineByNine[2][1] = posDown.getValue();
            nineByNine[2][2] = posDown.getNeighborValueNeg1IfEdge(Direction.RIGHT);
         }

      String sLS = System.getProperty("line.separator", "\r\n");
      return  "Middle position in 9x9 grid is *this*: " + toString() + sLS + 
         Arrays.toString(nineByNine[0]) + sLS + 
         Arrays.toString(nineByNine[1]) + sLS + 
         Arrays.toString(nineByNine[2]);
   }
}

它所没有的是“碰撞检测”或任何真正为您找到迷宫路径的东西。它只是在整个网格中移动,无论它是否穿过墙壁。,还可以有一些getNeighborIfNotWall(Direction)isWallToLeft()功能添加,但我会留给你。;)

(实际上,这些类无需做太多更改,就可以用于遍历任何2D数组,尽管我可能会添加对角线方向(例如UP_LEFT)和移动多个步骤的能力(例如getNeighbor(3, Direction.DOWN))。

这是一个演示用法:

public class MazePosDemo  {
   private static final int[][] MAZE_GRID = new int[][] {

   //mega maze grid goes here...

   };

   private static final MazePosition MAZE_HOLDER = new MazePosition(MAZE_GRID);

   public static final void main(String[] ignored)  {
      MazePosition pos = new MazePosition(0, 0);
      System.out.println("start: " + pos);

      pos = pos.getNeighbor(Direction.RIGHT);
      System.out.println("right: " + pos);

      pos = pos.getNeighbor(Direction.RIGHT);
      System.out.println("right: " + pos);

      pos = pos.getNeighbor(Direction.DOWN);
      System.out.println("down:  " + pos);

      pos = pos.getNeighbor(Direction.DOWN);
      System.out.println("down:  " + pos);

      pos = pos.getNeighbor(Direction.RIGHT);
      System.out.println("right: " + pos);

      pos = pos.getNeighbor(Direction.DOWN);
      System.out.println("down:  " + pos);

      pos = pos.getNeighbor(Direction.LEFT);
      System.out.println("left:  " + pos);

      pos = pos.getNeighbor(Direction.UP);
      System.out.println("up:    " + pos);

      pos = pos.getNeighbor(Direction.UP);
      System.out.println("up:    " + pos);

      System.out.println(pos.getNineByNine());
   }

}

输出量

[C:\java_code\]java MazePosDemo
start: [0,0]=1
right: [0,1]=1
right: [0,2]=1
down:  [1,2]=1
down:  [2,2]=1
right: [2,3]=1
down:  [3,3]=0
left:  [3,2]=1
up:    [2,2]=1
up:    [1,2]=1
Middle position in 9x9 grid is *this*: [1,2]=1
[1, 1, 1]
[0, 1, 0]
[0, 1, 1]

这是完整的源代码文件,其中包含上述所有内容(包括mega-maze-array):

   //Needed only by MazePosition
   import  java.util.Arrays;
   import  java.util.Objects;

enum Direction {
   UP(-1, 0),
   DOWN(1, 0),
   LEFT(0, -1),
   RIGHT(0, 1);
//config
   private final int rowSteps;
   private final int colSteps;
   private Direction(int rowSteps, int colSteps)  {
      this.rowSteps = rowSteps;
      this.colSteps = colSteps;
   }
   public int getNewRowIdx(int currentRowIdx)  {
      return  (currentRowIdx + getRowSteps());
   }
   public int getNewColIdx(int currentColIdx)  {
      return  (currentColIdx + getColSteps());
   }
   public int getRowSteps()  {
      return  rowSteps;
   }
   public int getColSteps()  {
      return  colSteps;
   }
};

class MazePosition  {
//config
   private static int[][] MAZE_GRID;
   private final int rowIdx;
   private final int colIdx;
//internal
   private final int rowIdxMinus1;
   private final int colIdxMinus1;
   public MazePosition(int[][] MAZE_GRID)  {
      if(this.MAZE_GRID != null)  {
         throw  new IllegalStateException("Maze double-array already set. Use x/y constructor.");
      }
      MazePosition.MAZE_GRID = MAZE_GRID;

      //TODO: Crash if null or empty, or sub-arrays null or empty, or unequal lengths, or contain anything but 0 or -1.

      rowIdx = -1;
      colIdx = -1;
      rowIdxMinus1 = -1;
      colIdxMinus1 = -1;
   }
   public MazePosition(int rowIdx, int colIdx)  {
      if(MazePosition.MAZE_GRID == null)  {
         throw  new IllegalStateException("Must set maze double-array with: new MazePosition(int[][]).");
      }

      if(rowIdx < 0  ||  rowIdx >= MazePosition.getRowCount())  {
         throw  new IllegalArgumentException("rowIdx (" + rowIdx + ") is invalid.");
      }
      if(colIdx < 0  ||  colIdx >= MazePosition.getColumnCount())  {
         throw  new IllegalArgumentException("colIdx (" + colIdx + ") is invalid.");
      }

      this.rowIdx = rowIdx;
      this.colIdx = colIdx;
      rowIdxMinus1 = (rowIdx - 1);
      colIdxMinus1 = (colIdx - 1);
   }

   public boolean isPath()  {  
      return  (getValue() == 0);  //1???
   }
   public int getValue()  {
      return  MazePosition.MAZE_GRID[getRowIdx()][getColumnIdx()];
   }
   public int getRowIdx()  {
      return  rowIdx;
   }
   public int getColumnIdx()  {
      return  colIdx;
   }
   public MazePosition getNeighbor(Direction dir)  {
      Objects.requireNonNull(dir, "dir");
      return  (new MazePosition(
         dir.getNewRowIdx(getRowIdx()), 
         dir.getNewColIdx(getColumnIdx())));
   }
   public MazePosition getNeighborNullIfEdge(Direction dir)  {
      if(isEdgeForDirection(dir))  {
         return  null;
      }
      return  getNeighbor(dir);
   }
   public int getNeighborValueNeg1IfEdge(Direction dir)  {
      MazePosition pos = getNeighborNullIfEdge(dir);
      return  ((pos == null) ? -1 : pos.getValue());
   }
   public static final int getRowCount()  {
      return  MAZE_GRID.length;
   }
   public static final int getColumnCount()  {
      return  MAZE_GRID[0].length;
   }
   public boolean isEdgeForDirection(Direction dir)  {
      Objects.requireNonNull(dir);
      switch(dir)  {
         case UP:    return isTopEdge(); 
         case DOWN:  return isBottomEdge(); 
         case LEFT:  return isLeftEdge();
         case RIGHT: return isRightEdge(); 
      }
      throw  new IllegalStateException(toString() + ", dir=" + dir);
   }
   public boolean isLeftEdge()  {
      return  (getColumnIdx() == 0);
   }
   public boolean isTopEdge()  {
      return  (getRowIdx() == 0);
   }
   public boolean isBottomEdge()  {
      return  (getRowIdx() == rowIdxMinus1);
   }
   public boolean isRightEdge()  {
      return  (getColumnIdx() == colIdxMinus1);
   }
   public String toString()  {
      return  "[" + getRowIdx() + "," + getColumnIdx() + "]=" + getValue();
   }
   public String getNineByNine()  {
      int[][] nineByNine = new int[3][3];

      //Middle row
         nineByNine[1][1] = getValue();
         nineByNine[1][0] = getNeighborValueNeg1IfEdge(Direction.LEFT);
         nineByNine[1][2] = getNeighborValueNeg1IfEdge(Direction.RIGHT);

      //Top
         MazePosition posUp = getNeighborNullIfEdge(Direction.UP);
         if(posUp != null)  {
            nineByNine[0][0] = posUp.getNeighborValueNeg1IfEdge(Direction.LEFT);
            nineByNine[0][1] = posUp.getValue();
            nineByNine[0][2] = posUp.getNeighborValueNeg1IfEdge(Direction.RIGHT);
         }

      //Bottom
         MazePosition posDown = getNeighborNullIfEdge(Direction.DOWN);
         if(posDown != null)  {
            nineByNine[2][0] = posDown.getNeighborValueNeg1IfEdge(Direction.LEFT);
            nineByNine[2][1] = posDown.getValue();
            nineByNine[2][2] = posDown.getNeighborValueNeg1IfEdge(Direction.RIGHT);
         }

      String sLS = System.getProperty("line.separator", "\r\n");
      return  "Middle position in 9x9 grid is *this*: " + toString() + sLS + 
         Arrays.toString(nineByNine[0]) + sLS + 
         Arrays.toString(nineByNine[1]) + sLS + 
         Arrays.toString(nineByNine[2]);
   }
}
public class MazePosDemo  {
   private static final int[][] MAZE_GRID = new int[][] {
      {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,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,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}};
   private static final MazePosition MAZE_HOLDER = new MazePosition(MAZE_GRID);

   public static final void main(String[] ignored)  {
      MazePosition pos = new MazePosition(0, 0);
      System.out.println("start: " + pos);

      pos = pos.getNeighbor(Direction.RIGHT);
      System.out.println("right: " + pos);

      pos = pos.getNeighbor(Direction.RIGHT);
      System.out.println("right: " + pos);

      pos = pos.getNeighbor(Direction.DOWN);
      System.out.println("down:  " + pos);

      pos = pos.getNeighbor(Direction.DOWN);
      System.out.println("down:  " + pos);

      pos = pos.getNeighbor(Direction.RIGHT);
      System.out.println("right: " + pos);

      pos = pos.getNeighbor(Direction.DOWN);
      System.out.println("down:  " + pos);

      pos = pos.getNeighbor(Direction.LEFT);
      System.out.println("left:  " + pos);

      pos = pos.getNeighbor(Direction.UP);
      System.out.println("up:    " + pos);

      pos = pos.getNeighbor(Direction.UP);
      System.out.println("up:    " + pos);

      System.out.println(pos.getNineByNine());
   }

}


 类似资料:
  • 我正在尝试寻找到EndPotion的路径。这是一个递归函数。请帮助,我要自杀了。 这是给定的地图 我想递归地使用GetPath来到达上面地图中的EndPotion。参数是当前位置、结束位置和地图。对于这个例子,起始位置是(0,0)和结束,EndPotionis是(0,3),右上角。0代表墙壁,1代表路径。 我需要返回一个包含有效点的arraylist到结束位置。虽然我的数组大小始终为0,并且基本大

  • 给定一个填充了0和1的二维字符数组,其中0代表一堵墙,1代表一条有效路径,我开发了一个名为findPath(int r,int c)的递归方法,用于在标有“x”的迷宫中找到出口。该方法接收迷宫的当前行和列,并通过N、E、S、W方向,直到找到有效路径,并用“”标记该有效路径。假设所有方向都被一堵墙挡住,那么该方法假设是回溯,直到情况不再如此,然后用“F”标记该路径,以表示坏路径。 现在我不明白为什么

  • 问题内容: 为了编写一个解决C程序的蛮力迷宫,我首先编写了这个Java程序来测试一个想法。我对C还是很陌生,打算在Java中正确处理后将其转换。结果,我试图远离arraylist,fancy库等,以便更轻松地转换为C。该程序需要生成最短步骤的单个宽度路径来解决迷宫问题。我认为我的问题可能在于将通过每个递归传递的路径存储数组碎片化。感谢您的关注。-乔 代码中解释了数字符号 问题答案: 这本来不是要作

  • 我得到了一些构建迷宫的代码,以及任何其他需要的东西,抽象迷宫类包含一个抽象方法“makeMove(int row,int col)”。这是我试图编写的解决迷宫的方法,向左、向右、向上、向下移动。 我刚刚开始做这件事,下面是我到目前为止的全部资料。 好的,我让代码运行到不再出现错误的地方。 感谢所有的帮助。

  • 我的任务是用回溯和递归的方法解决一个迷宫。这更多的是一个关于这个概念的概念问题。 回溯电话是如何接通的?从我所看到的所有示例来看,似乎递归总是在回溯步骤之前立即调用,所以回溯是无法实现的。谁能给我解释一下回溯步骤是怎么达到的?

  • 我在用递归解迷宫。我的矩阵是这样的 这是更大矩阵的原型。我的求解递归方法如下所示 你们可以注意到,这里我返回一个布尔值,如果我找到一条路径,它应该会给我一个真值。但它总是给我错误的答案。我不确定我在递归方法中犯的逻辑错误。方法如下 endX=3;endY=10;