Java递归迷宫求解器问题 [英] Java Recursive Maze Solver problems

查看:92
本文介绍了Java递归迷宫求解器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用递归编写迷宫求解器,似乎它尝试每个方向一次,然后停止,我无法弄清楚为什么。如果您发现问题,请告诉我。
Key
0是开放空间
1是墙
2是路径的一部分
3是迷宫的终点

I am trying to write a maze solver using recursion, and it seems that it tries each direction once, then stops and I can't figure out why. If you see a problem, please let me know. Key 0 is an open space 1 is a wall 2 is part of the path 3 is the end of the maze

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,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1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1},
      {1,0,0,0,0,0,0,0,1,0,1,0,1,0,1,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,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迷宫网格引起了我的兴趣,我想出了一个类,它应该让它更简单。整个更简单。如果您需要进行递归,那么它对您没有用,但如果您可以使用它,它会消除它的大部分复杂性。

You have asked four questions about this maze-recursion puzzle in the past five hours, which attests to how complicated it is. This whole concept a 1/0 maze-grid has intrigued me, and I've come up with a class that should make it a whole lot simpler. If you're required to do recursion, then it will not be useful to you, but if you can use it, it eliminates much of its complexity.

有两个类和一个枚举。

首先,枚举,定义你想要在网格中移动的方向,并一次一个地确定新索引,基于它的运动。

First, the enum, which defines the direction you want to move in the grid and determines the new indexes, one-at-a-time, based on its movement.

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 [] [] 构造函数将迷宫网格双数组设置到其中,并静态存储该实例:

The main class is called MazePosition (below). First you set the maze-grid double-array into it, via its int[][] constructor, and store that instance statically:

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

(此步骤设计得更好,但有效。)

(This step could be designed better, but it works.)

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

After setting the maze-grid (which is a one-time-only thing, per-execution), then the x/y constructor is used to declare an initial position:

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数组应该包含一位布尔值,而不是4字节整数 ,但在数组的代码中查找对于 int s是有意义的,而不是与布尔值...注意它至少应该更改到字节 s。)

The value of each position is retrieved by pos.getValue() or pos.isPath()--I think 1 is a "wall" and 0 is the "path". (As an aside: The huge 2d-array should really contain one-bit booleans, instead of 4-byte ints, but looking at the array's code makes sense with ints, and doesn't with booleans... Note that it should at least be changed to bytes.)

所以关于移动,如果你试图找到一个没有的邻居,当在左边缘向左移动时,抛出 IllegalStateException 。使用 is * Edge()函数来避免这种情况。

So regarding movement, if you attempt to get a neighbor when there is none, such as moving left at the left edge, an IllegalStateException is thrown. Use the is*Edge() functions to avoid this.

MazePosition类还有一个方便的调试函数叫做 getNin​​eByNine(),返回数组值的9x9网格(作为字符串),其中中间项是当前位置。

The MazePosition class also has a convenient debugging function called getNineByNine(), which returns a 9x9 grid of the array values (as a string), where the middle item is the current position.

   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()函数,但我会留给你。 ;)

What this does not have, is "collision detection" or anything that actually figures out the maze-path for you. It just moves throughout the grid, regardless if it's moving through walls or not. There could easily be some getNeighborIfNotWall(Direction) and isWallToLeft() functions added, but I'll leave that to you. ;)

(实际上,这些类没有太多变化,可以用来遍历任何2D数组,虽然我可能会添加对角线方向,例如 UP_LEFT ,以及移动多个步骤的功能,例如 getNeighbor(3,Direction.DOWN))。

(Actually, these classes, without too much change, could be used to traverse any 2D array, although I'd probably add diagonal directions, such as UP_LEFT, and the ability to move multiple steps, such as 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):

And here's the entire source-code file, containing all of the above (including the 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,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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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());
   }

}

这篇关于Java递归迷宫求解器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆