简体   繁体   English

Java在数组中搜索重复模式

[英]Java searching an array for repeating pattern

I have a program that is searching a maze to find the best way out. 我有一个程序正在搜索迷宫,以找到最佳的出路。 As it searches it adds the next move to an array. 搜索时,将下一步移动到数组中。 My problem is that it keeps repeating the same three moves over and over. 我的问题是,它不断重复相同的三个动作。 I need to find the best way to check that array of moves in order to force it to change move when a loop has been detected. 我需要找到检查移动数组的最佳方法,以便在检测到循环时强制其更改移动。

edit for clarity, http://www.logicmazes.com/theseus.html maze three is the one I'm testing. 为了清楚起见, 编辑http://www.logicmazes.com/theseus.html迷宫3。 what happens is that it gets stuck moving up and down in the column it starts in. 发生的事情是它在开始的列中被卡住了上下移动。

You could keep track of every cell your current path has already visited, and not go to those cells again (since that would create a loop). 您可以跟踪当前路径已访问的每个单元,而不必再次转到这些单元(因为那样会形成循环)。

As far as the data structure is concerned, I see two main possibilities: 就数据结构而言,我看到两种主要可能性:

  • keep an array -- or a set -- of coordinates that you've visited; 保留您访问过的一组数组或一组坐标; or 要么
  • have a boolean array of the same dimensions as the maze, setting the visited cells to true . 具有与迷宫相同维度的布尔数组,将访问的像元设置为true

You would need to update the structure whenever you take a step, and whenever you backtrack. 只要您采取任何步骤,并且每当您回溯时,都需要更新结构。

If you are using an Array then you should be able to use "contains" method to check if the move is already in the array. 如果使用的是数组,则应该可以使用“包含”方法来检查移动是否已在数组中。

In order to do that you may have to modify your equals method to compare two different moves to check if they are the same. 为此,您可能必须修改equals方法以比较两个不同的动作以检查它们是否相同。

In that case, when you identify the same move, you can ignore that and look for other moves. 在这种情况下,当您确定相同的动作时,您可以忽略该动作并寻找其他动作。 An example pseudo code 伪代码示例

public class Move {
  int x;
  int y;

  public boolean equals(Object o){
    if(o == null) return false;
    if(!(o instanceof) Move) return false;

    Move other = (Move) o;
    if(this.x != other.x) return false;
    if(this.y != other.y)   return false;

    return true;
  }
}

public class SolveMaze {
    List<Move> moves;
    ...
    public boolean isValidMove(Move move) {
        if (moves.contains(move)) return false;
        else {
            ...
                moves.add(move);
        }
    }
}

It sounds like the problem is that your "state" doesn't actually contain enough state information. 听起来问题是您的“状态”实际上没有包含足够的状态信息。 In every cycle, after Theseus has moved and the Minotaur has moved twice, the state consists of the following: 在每个周期中,These修斯移动并且牛头怪移动了两次之后,该状态包括以下内容:

  • Theseus's x-coordinate. us修斯的x坐标。
  • Theseus's y-coordinate. us修斯的y坐标。
  • The Minotaur's x-coordinate. 牛头怪的x坐标。
  • The Minotaur's y-coordinate. 牛头怪的y坐标。

You can represent these as some sort of MazeState object whose equals and hashCode methods make it easy to see if two instances represent the same state. 您可以将它们表示为某种MazeState对象, MazeState对象的equalshashCode方法可以轻松查看两个实例是否表示相同状态。

Since the Minotaur's motion follows a very rigid program, every move that Theseus makes (left/right/up/down/delay) will move from one well-defined state to another. 由于Minotaur的动作遵循非常严格的程序,因此,These修斯进行的每一个动作(左/右/上/下/延迟)都会从一种定义明确的状态转移到另一种状态。 You then need to forbid Theseus from making any moves that: 然后,您需要禁止These修斯采取任何行动:

  • Would cause the Minotaur's x- and y-coordinates to equal those of Theseus (since this means Theseus is dead). 将导致牛头怪的x和y坐标等于These修斯的坐标(因为这意味着this修斯已经死了)。
  • Would cause the new state to be equal to a state that you've previously been in (since this means that no progress has been made). 会使新状态等于您以前所处的状态(因为这意味着尚未取得任何进展)。

To do this, you can store the previous states in a HashSet<MazeState> . 为此,您可以将以前的状态存储在HashSet<MazeState>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM