如何自由地遍历由主方向在二维数组中的元素? (向下,向上,左,右) [英] How to freely traverse the elements in a two-dimensional array by cardinal direction? (DOWN, UP, LEFT, RIGHT)

查看:138
本文介绍了如何自由地遍历由主方向在二维数组中的元素? (向下,向上,左,右)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是有关通过迷宫辩别的路径,因为是由一个二维psented重新$ P $数组。例如,通过这个迷宫的路径

This question is about figuring the path through a maze, as is represented by a two-dimensional array. For example, the path through this maze

   0 1 2 3 4
   ---------
0| 1 0 1 1 1
1| 1 0 0 0 1
2| 1 0 1 0 1
3| 1 1 1 0 1
4| 1 1 1 0 1

START: [0,1]
Down:  [1,1]
Right: [1,2]
Right: [1,3]
Down:  [2,3]
Down:  [3,3]
Down:  [4,3]

海报试图这样做纯粹是递归,居然张贴了关于这四个不同的问题,五个小时内访问 - 这讲它的复杂性。

The poster was attempting to do this purely with recursion, and actually posted four different questions about it within five hours--which speaks to its complexity.

这让我的思维更通用的问题:你如何随意的元素中移动在二维数组,在任意主方向?下,上,左,右

This got me thinking of the more generic question: How do you freely move around the elements in a two-dimensional array, in any cardinal direction? DOWN, UP, LEFT, RIGHT

如果这个问题是可以解决的,然后穿越迷宫的就好办多了。

If this problem can be solved, then traversing a maze would be much easier.

推荐答案

我解决了这个有两个班,一个枚举。

I solved this with two classes, and an Enum.

首先,枚举,它定义要移动的方向,并确定新的指标,一个在-A-时间,是根据它的运动。

First, the enum, which defines the direction you want to move 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;
   }
};

主要类叫做 GridPosition (下图)。首先,你设置双阵列电网伸进去,通过其 INT [] [] 构造函数,静态存储例如:

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

private static final GridPosition GRID_HOLDER = new GridPosition(GRID);

(这个步骤可以设计更好,但它的工作原理。此外,应该接受的任何类型的)。​​

(This step could be designed better, but it works. Also, it should accept any type.)

设置网格(其是一次性唯一,每次执行),则X / Y构造用于声明一个初始位置之后:

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

GridPosition pos = new GridPosition(0, 0);

在这之后,只要将必要的:

And after that, just move as necessary:

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

每个位置的值由 pos.getValue检索()。 (顺便说一句:用于迷宫巨大的二维阵列 - 在这个岗位的底部,在全code - 真的应该包含一个位布尔值,而不是4个字节的整数,但寻找的在数组的code有意义与 INT S,并且不与布尔值...请注意,它至少应该改为字节秒)

The value of each position is retrieved by pos.getValue(). (As an aside: The huge 2d-array used for the maze--at the bottom of this post, in the "full code"--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异常被抛出。使用是*边缘()函数来避免这个问题。

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.

该GridPosition类也有称为方便的调试功能 getNin​​eByNine(),它返回数组值的一个9x9的网格(作为一个字符串),其中中间产品当前位置。

The GridPosition 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 GridPosition  {
//state
   private static int[][] GRID;
   private final int rowIdx;
   private final int colIdx;
//internal
   private final int rowIdxMinus1;
   private final int colIdxMinus1;
   public GridPosition(int[][] GRID)  {
      if(this.GRID != null)  {
         throw  new IllegalStateException("Grid already set. Use x/y constructor.");
      }
      GridPosition.GRID = 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 GridPosition(int rowIdx, int colIdx)  {
      if(GridPosition.GRID == null)  {
         throw  new IllegalStateException("Must set grid with: new GridPosition(int[][]).");
      }

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

      this.rowIdx = rowIdx;
      this.colIdx = colIdx;
      rowIdxMinus1 = (rowIdx - 1);
      colIdxMinus1 = (colIdx - 1);
   }
   public int getValue()  {
      return  GridPosition.GRID[getRowIdx()][getColumnIdx()];
   }
   public int getRowIdx()  {
      return  rowIdx;
   }
   public int getColumnIdx()  {
      return  colIdx;
   }
   public GridPosition getNeighbor(Direction dir)  {
      Objects.requireNonNull(dir, "dir");
      return  (new GridPosition(
         dir.getNewRowIdx(getRowIdx()),
         dir.getNewColIdx(getColumnIdx())));
   }
   public GridPosition getNeighborNullIfEdge(Direction dir)  {
      if(isEdgeForDirection(dir))  {
         return  null;
      }
      return  getNeighbor(dir);
   }
   public int getNeighborValueNeg1IfEdge(Direction dir)  {
      GridPosition pos = getNeighborNullIfEdge(dir);
      return  ((pos == null) ? -1 : pos.getValue());
   }
   public static final int getRowCount()  {
      return  GRID.length;
   }
   public static final int getColumnCount()  {
      return  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
         GridPosition 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
         GridPosition 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 GridPosDemo  {
   private static final int[][] GRID = new int[][] {

   //mega grid goes here...

   };

   private static final GridPosition GRID_HOLDER = new GridPosition(GRID);

   public static final void main(String[] ignored)  {
      GridPosition pos = new GridPosition(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 GridPosDemo
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]

为了使用这个通过一个迷宫穿越了正确的路径,这就需要添加到它(因此它不穿过墙壁),除了一些碰撞检测来跟踪你去过的地方(这样你就不会结束回到启动位置)。如一些 getNeighborIfNotWall(方向) isWallToLeft()功能。

In order to use this for traversing the correct path through a maze, this needs "collision detection" added to it (so it doesn't go through walls), in addition to something to keep track of where you've been (so you don't end up back at the start-position). Such as with some getNeighborIfNotWall(Direction) and isWallToLeft() functions.

暂且不论迷宫问题,我会考虑到这些类完成之前做到以下几点:

Putting aside the maze-question, I would do the following before considering these classes complete:


  • 请在阵列式通用的,而不是整数

  • 添加错误检查,记载于code

  • 重新设计,你如何设置网格。

  • 添加对角线方向: UP_LEFT UP_RIGHT DOWN_LEFT DOWN_RIGHT

  • 添加到移动在一个方向多个步骤的能力: getNeighbor(3 Direction.DOWN)

  • Make the array-type generic, instead of ints
  • Add error checking, as documented in the code
  • Re-design how you set the grid.
  • Add diagonal directions: UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT
  • Add the ability to move multiple steps in one direction: getNeighbor(3, Direction.DOWN)

下面就是整个源 - code文件,包含所有以上(包括巨型迷宫格):

Here's the entire source-code file, containing all of the above (including the mega-maze grid):

   //Needed only by GridPosition
   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 GridPosition  {
//config
   private static int[][] GRID;
   private final int rowIdx;
   private final int colIdx;
//internal
   private final int rowIdxMinus1;
   private final int colIdxMinus1;
   public GridPosition(int[][] GRID)  {
      if(this.GRID != null)  {
         throw  new IllegalStateException("Grid already set. Use x/y constructor.");
      }
      GridPosition.GRID = 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 GridPosition(int rowIdx, int colIdx)  {
      if(GridPosition.GRID == null)  {
         throw  new IllegalStateException("Must set grid with: new GridPosition(int[][]).");
      }

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

      this.rowIdx = rowIdx;
      this.colIdx = colIdx;
      rowIdxMinus1 = (rowIdx - 1);
      colIdxMinus1 = (colIdx - 1);
   }
   public int getValue()  {
      return  GridPosition.GRID[getRowIdx()][getColumnIdx()];
   }
   public int getRowIdx()  {
      return  rowIdx;
   }
   public int getColumnIdx()  {
      return  colIdx;
   }
   public GridPosition getNeighbor(Direction dir)  {
      Objects.requireNonNull(dir, "dir");
      return  (new GridPosition(
         dir.getNewRowIdx(getRowIdx()),
         dir.getNewColIdx(getColumnIdx())));
   }
   public GridPosition getNeighborNullIfEdge(Direction dir)  {
      if(isEdgeForDirection(dir))  {
         return  null;
      }
      return  getNeighbor(dir);
   }
   public int getNeighborValueNeg1IfEdge(Direction dir)  {
      GridPosition pos = getNeighborNullIfEdge(dir);
      return  ((pos == null) ? -1 : pos.getValue());
   }
   public static final int getRowCount()  {
      return  GRID.length;
   }
   public static final int getColumnCount()  {
      return  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
         GridPosition 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
         GridPosition 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 GridPosDemo  {
   private static final int[][] 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,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,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1,1,1,1,1,1,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}};
   private static final GridPosition GRID_HOLDER = new GridPosition(GRID);

   public static final void main(String[] ignored)  {
      GridPosition pos = new GridPosition(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());
   }

}

这篇关于如何自由地遍历由主方向在二维数组中的元素? (向下,向上,左,右)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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