A rat is placed in a maze at some unknown position in the maze.
All we can go is in up, down, right or left directions. And we have two methods:
- tryMove(<direction>) which returns false if there is a wall and true if we can move.
- bool hasLadder(): which returns true if there is a ladder to escape.
We have to write a function explore which returns true if we find the way out or false if there is no way.
This is a simple graph problem and can be solved using bfs or a dfs algorithm if we can find mark these places. If we cannot mark these places we can move in cycles visiting the same places. Can some one help me to get rat out of the maze please if its unmarked? Is it possible?
Both breadth-first and depth first search require memory and a naive algorithm can loop indefinitely. A rat probably only has O(1) memory.
One way to solve it without remembering where you have been is to pick a direction at random. The solve time will be extremely long, but it should eventually reach every reachable square. This is related to the 2D random walk.