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. what happens is that it gets stuck moving up and down in the column it starts in.
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:
You can represent these as some sort of
MazeStateobject whoseequalsandhashCodemethods make it easy to see if two instances represent the same state.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. You then need to forbid Theseus from making any moves that:
To do this, you can store the previous states in a
HashSet<MazeState>.