A rat trapped in a maze. Initially it has to choose one of two directions. If it goes to the right,
then it will wander around in the maze for 3 minutes and will return to its initial position. If it
goes to left, then with probability 1/3 it will depart the maze after 2 minutes of traveling, and
with probability 2/3 it will return to its initial position after 5 minutes of traveling. Assuming
that the rat is at all times equally likely to go to the left or the right, what is expected number
of minutes that it will be trapped in the maze?
Answers
Consider a rat that is placed at (0, 0) in a square matrix m[][] of order n and must get to the destination at (n-1, n-1). The goal is to discover a sorted array of strings that represent all of the different routes that the rat can take to reach the destination (n-1, n-1). The rat may travel in the following directions: 'U' (up), 'D' (down), 'L' (left), and 'R' (right) (right).
Explanation:
Approach:
Begin from the beginning index (i.e. (0,0)) and look for valid moves throughout the grid's neighbouring cells in the order Down->Left->Right->Up (to obtain the sorted routes).
If the move is possible, move to that cell while saving the character associated with the move (D,L,R,U) and continue the search for the legal move until the last index (i.e. (n-1,n-1)) is reached.
Also, continue to mark cells as visited, and once we've travelled all of the roads possible from that cell, unmark that cell for other paths and erase the character from the path constructed.
Save the path you followed when you reach the last index of the grid (bottom right).