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 as1and0respectively 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]
]

The total number of unique paths is2.

Note: m and n will be at most 100.

题意:增加障碍,不能到达障碍,不能越过障碍。

思路:思路和unique paths是一样的,只是要判断当前值是否为1,若是,则其对应的dp数组中赋值为0,不是时,状态方程是:dp[i][j]=dp[i-1][j]+dp[i][j-1]。代码如下:

 class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid)
{
int m=obstacleGrid.size(),n=obstacleGrid[].size();
vector<vector<int>> dp(m,vector<int>(n,));
if(m==||n==) return ;
if(obstacleGrid[][]==) return ;
dp[][]=; //初始行
for(int i=;i<m;++i)
{
if(obstacleGrid[i][] ==)
{
break;
}
else
dp[i][]=;
}
//初始列
for(int i=;i<n;++i)
{
if(obstacleGrid[][i] ==)
{
break;
}
else
dp[][i]=;
} for(int i=;i<m;++i)
{
for(int j=;j<n;++j)
{
if(obstacleGrid[i][j] !=)
dp[i][j]=dp[i-][j]+dp[i][j-];
}
} return dp[m-][n-];
}
};

当用一维数组去简化时,要注意些问题,仅一行,或者仅一列时,还要依次的确定数组dp[]中对应的值,不是像上一题那样简单赋值为1就行,所以,遍历时,行和列的起始点都是从0开始;另外也得注意的是,数组dp中的当前的前一个是否存在,即, j >0。代码如下:

 class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid)
{
int row=obstacleGrid.size(),col=obstacleGrid[].size();
if(obstacleGrid[][]==) return ;
vector<int> dp(col,);
dp[]=; for(int i=;i<row;++i)
{
for(int j=;j<col;++j)
{
if(obstacleGrid[i][j]==)
dp[j]=;
else if(j>)
dp[j]+=dp[j-];
}
}
return dp[col-];
}
};

[Leetcode] unique paths ii 独特路径的更多相关文章

  1. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  3. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  4. LEETCODE —— Unique Paths II [Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  5. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  7. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. 063 Unique Paths II 不同路径 II

    这是“不同路径” 的进阶问题:现在考虑网格中有障碍物.那样将会有多少条不同的路径从左上角到右下角?网格中的障碍物和空位置分别用 1 和 0 来表示.例如,如下所示在 3x3 的网格中有一个障碍物.[  ...

随机推荐

  1. mongo数据库相关目录

    mongodb的docker化安装 mongodb的windows系统下安装 grafana使用Prometheus数据源监控mongo数据库 mongodb副本集的docker化安装 mongodb ...

  2. Codeforces Round #500 (Div. 2) BC

    CodeForces 1013B And CodeForces 1013C  Photo of The Sky B 可以发现只有一次与操作是有意义的,所以答案只有-1,0,1,2四种情况 #inclu ...

  3. 关于 js 对象 转 字符串 和 深拷贝 的探讨

    随着更多语言的支持 **json** 作为数据传输和存储的媒体,已经非常成熟且应用广泛.却存在致命硬伤,不携带 **对象方法** .在数据传输和存储中,这是恰当的和合理的. 但是在更多的应用场景中,又 ...

  4. VS中的快捷键

    1.代码中追踪函数的详细代码:   F12

  5. 3155: Preprefix sum

    3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...

  6. guacamole实现虚拟键盘

    要做的事情比较简单,就是先实例化一个虚拟键盘,然后监听事件即可. js代码 //虚拟键盘数据 var a = {"language":"en_US"," ...

  7. vue循环绑定v-model

    直接上代码 结构: <repayInput v-if="formData" v-for="(item, index) in formData" :isPw ...

  8. join ,left join ,right join有什么区别

    join等价于inner join内连接,是返回两个表中都有的符合条件的行. left join左连接,是返回左表中所有的行及右表中符合条件的行.(左表为主表) right join右连接,是返回右表 ...

  9. tp5.0 模型查询数据的返回类型,分页

    一开始用painate()这个函数的时候,发现有的查询方式不能使用这个函数,由此了解到了模型查询和普通查询返回类型的不同 1.原生查询方法 Db::query("select * from ...

  10. Lua工具类

    1.打印table --一个用以打印table的函数 function print_r (t, name) print(pr(t,name)) end function pr (t, name, in ...