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. Hadoop(20)-MapReduce框架原理-OutputFormat

    1.outputFormat接口实现类 2.自定义outputFormat 步骤: 1). 定义一个类继承FileOutputFormat 2). 定义一个类继承RecordWrite,重写write ...

  2. ruby Encoding

    一. 查看ruby支持的编码 Encoding.name_list 二. 搜索编码 Encoding.find('US-ASCII') #=> US-ASCII,不存在则抛出异常 三. __EN ...

  3. Horner规则求多项式

    /* Horner */ /*多项式:A(x)=a[n]X^n+a[n-1]x^n-1+...+a[1]X^1+a[0]X^0*/ #include <stdio.h> long int ...

  4. [Cracking the Coding Interview] 4.6 Successor 后继节点

    Write an algorithm to find the 'next' node(i.e. in-order successor) of a given node in a binary sear ...

  5. ActivityStream是什么?什么是Feed流?

    我先说说feed流吧,它就是社交网站中用户活动信息流,例如用户写了博客.发了照片.评论了什么等等.Facebook叫newsFeed.推特叫TimeLineFeed.ActivityStream是这些 ...

  6. python--基本类型之列表

    Lest(列表): 定义和创建列表: 列表:是python以及其他语言中最常用的数据结构之一.python用 [] 来解析列表列表是可变的.--可以改变列表的内容可以用切片 a=['张三','李四', ...

  7. WPF中使用定时器的注意事项

    原文:WPF中使用定时器的注意事项 注意事项 要使用System.Windows.Threading.DispatcherTimer,而不能使用System.Timers.Timer. 原因是WPF是 ...

  8. 基于Ubuntu Server 16.04 LTS版本安装和部署Django之(四):安装MySQL数据库

    基于Ubuntu Server 16.04 LTS版本安装和部署Django之(一):安装Python3-pip和Django 基于Ubuntu Server 16.04 LTS版本安装和部署Djan ...

  9. Delphi中客户端获取数据库更新信息(更新条数)

    1.SQL语句 from tb where xxx='XXX') //不存在,则插入数据 begin insert into tb(xxx) values('XXX') //这里自己定义,插入或更新都 ...

  10. react实现页面切换动画效果

    一.前情概要 注:(我使用的路由是react-router4)     如下图所示,我们需要在页面切换时有一个过渡效果,这样就不会使页面切换显得生硬,用户体验大大提升:     but the 问题是 ...