一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。

现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

网格中的障碍物和空位置分别用 1 和 0 来表示。

说明:m 和 n 的值均不超过 100。

示例 1:

输入: [   [0,0,0],   [0,1,0],   [0,0,0] ] 输出: 2 解释: 3x3 网格的正中间有一个障碍物。 从左上角到右下角一共有 2 条不同的路径: 1. 向右 -> 向右 -> 向下 -> 向下 2. 向下 -> 向下 -> 向右 -> 向右

因为机器人只能往下或者往右走,判断哪些地方根本到不了,给他取值为0,再dp

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

Leetcode63.Unique Paths II不同路径2的更多相关文章

  1. 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). ...

  2. leetcode-63. Unique Paths II · DP + vector

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

  3. [Leetcode] unique paths ii 独特路径

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

  4. LeetCode63 Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  5. 063 Unique Paths II 不同路径 II

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

  6. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  7. [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 ...

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

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

  9. 【LeetCode练习题】Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

随机推荐

  1. 2019/10/24 CSP-S 模拟

    T1 tom 题意: 考虑一定是属于\(a\)的在一坨,属于\(b\)的在一坨,找到这条连接\(a\)和\(b\)的边,然后分别直接按\(dfs\)序染色即可 注意属于\(a\)的连通块或属于\(b\ ...

  2. 第二周——1.项目中MySQL版本问题

    1.版本升级 经组长推荐,本地安装的是mysql-8.0.11,而主项目用的还是版本5.6, 因此需要升级版本. 首先,更新驱动:下载mysql-connector-java-8.0.11,将E:\P ...

  3. SpringIOC自定义属性编辑器PropertyEditor

    Spring中我们可以使用属性编辑器来将特定的字符串转换为对象 String--转换-->object java.beans.PropertyEditor(JDK中的接口)用于将xml文件中字符 ...

  4. Python学习day45-数据库(总结)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. Python 中的运算符

    1.算数运算符 + 加 - 减 * 乘 计算字符串重复的次数 print("唯美" * 10) / 除 round(10/3, 4)   4代表位数 // 取整数 % 取余数 ** ...

  6. <每日一题>题目25:快速排序

    ''' 快速排序:分而治之,一分为二进行排序 ''' import cProfile import random def quick_sort(nums): if len(nums) <= 1: ...

  7. js如何往数组Array中添加元素 (2013-09-04 10

    unshift:将参数添加到原数组开头,并返回数组的长度 pop:删除原数组最后一项,并返回删除元素的值:如果数组为空则返回undefined push:将参数添加到原数组末尾,并返回数组的长度 co ...

  8. .Net Email操作类

    using System; using System.Text; using System.Net.Mail; using System.Net; using System.Linq; using S ...

  9. Mahout In Action-第一章:初识Mahout

    1. 初识Mahout 本章涵盖以下内容: Apache Mahout是什么? 现实中推荐系统引擎.聚类.分类概述 配置mahout 读者可能从本书的标题中猜测到,本书是一本讲解如何将mahout应用 ...

  10. [Cqoi2015] 编号 【逆向思维,暴力枚举】

    Online Judge:Luogu-P4222 Label:逆向思维,暴力枚举 题目描述 你需要给一批商品编号,其中每个编号都是一个7位16进制数(由0~9, a-f组成).为了防止在人工处理时不小 ...