[英]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: 就数据结构而言,我看到两种主要可能性:
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修斯移动并且牛头怪移动了两次之后,该状态包括以下内容:
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
对象的equals
和hashCode
方法可以轻松查看两个实例是否表示相同状态。
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修斯采取任何行动:
To do this, you can store the previous states in a HashSet<MazeState>
. 为此,您可以将以前的状态存储在
HashSet<MazeState>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.