Problem
1. A robot is located at the top-left corner of a m x n grid. The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid. How many possible unique paths are there?
2. Follow up, now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example,There is one obstacle in the middle of a 3x3 grid as illustrated below. The total number of unique paths is 2.
2. Follow up, now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example,There is one obstacle in the middle of a 3x3 grid as illustrated below. The total number of unique paths is 2.
[ [0,0,0], [0,1,0], [0,0,0] ]
Algorithm
Dynamical Programming
Let dp[i][j] be the number of ways to reach A[i][j].
dp[i][j] = 0 if A[i][j] is blocked
d[i-1][j] + dp[i][j-1], if no blocks
No comments:
Post a Comment