Leetcode之Unique Paths II 问题

问题描述:

Follow up for "Unique Paths":

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.

[

  [0,0,0],

  [0,1,0],

  [0,0,0]

]

问题来源:Unique Paths II (详细地址:https://leetcode.com/problems/unique-paths-ii/description/)

思路分析:Unique Paths 和这道题最大的区别就是,这道题设置了障碍,也就是说遇到1的时候,表示此路是行不通的,但是万变不离其宗,都是动态规划来解决,而且这里不当都设置第0行和第0列的值了,在这只设置dp[0] = 1,设置成1代表着只能是往右走第一行和往下走第一列,只有这一种选择,不能有别的方案。

代码:

Leetcode之Unique Paths II 问题