Objective: Given two dimensional matrix, write an algorithm to count all possible paths from top left corner to bottom-right corner. You are allowed to move only in two directions, move right OR move down. There are few obstructions as well, means few cells are blocked and you cannot travel that cell.

Many times this problem is being referred as “Robot Travel Problem“. Given a 2d matrix, how many ways a robot can travel from top left corner to bottom right corner and there are few cells in which robot cannot travel.

All we need to take care if one extra condition that we cannot travel to the blocked cells.

In recursive solution given in “Count all paths from top left to bottom right of a mXn matrix” just check for condition if cell is not blocked.

In Dynamic programming solution, we need to take care of two conditions, first we are not solving it for blocked cells and while solving for other cells do not involve blocked cells.

See the code for better understanding.

Code:

Output:

No Of Path (Recursion):- 1
No Of Path (DP):- 1

__________________________________________________ Top Companies Interview Questions..-

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________