It's funny that @mousey will ask about the rat ... well.
I have seen this problem creep several times already.
The first (and naive) solution is to follow the left (or right) wall, however it is possible to create special labyrinths where the rat ends in circles.
In fact, for each deterministic order of moves (e.g. 2L, 1R, 2L, 1R, ...) that I tried (when I was in high school, I had time), we could come up with a maze that would make the rat run in a circle . Of course, the harder the cycle, the harder the maze.
Therefore, if you have O (1) memory, the only solution to exit the maze is the random walk suggested by Mark Byers. Unfortunately, you cannot prove that it is impossible to exit the maze using such an algorithm.
On the other hand, if you have O (N) memory, it comes down to creating a map (somehow). The strategy that I finally came up with then was to leave the pheromone (increasing the counter) in each case that I went through, and when you have a choice in motion, to choose among the least visited cases. However, it was always possible to exit, so I never had to think about the state of termination if there was no way out.
You can also use the fill fill algorithm ... Of course, that was before I found out about the BFS terms. This has the advantage that you have a termination condition (when there is not a single case left to study).
Matthieu M.
source share