I was recently asked this question in an Interview:
A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to reach destination. The rat can move only in two directions: forward and down. (or 4 in advanced problem). In the maze matrix, 0 means the block is dead end and 1 means the block can be used in the path from source to destination.
My answer was the one mentioned in the link, which is using backtracking. High level pseudo code from the link:
If destination is reached
print the solution matrix
Else
a) Mark current cell in solution matrix as 1.
b) Move forward in horizontal direction and recursively check if this
move leads to a solution.
c) If the move chosen in the above step doesn't lead to a solution
then move down and check if this move leads to a solution.
d) If none of the above solutions work then unmark this cell as 0
(BACKTRACK) and return false.
The interviewer was quite surprised by my response , and was apparently expecting a polynomial time solution.
Does there exist a polynomial time solution for this problem?