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. redis 带入的挖矿病毒 qW3xT.2 wnTKYg 解决方法

    最近我的阿里云ecs 老是收到 云盾态势感知系统检测到异常 top -c 后发现一个 疑似病毒  /tmp/qW3xT.2 看到网友们的解决方案 试过之后效果不错,可以用的 知道wnTKYg是什么鬼之 ...

  2. jquery easyui alert闪一下的问题

    最近做项目使用了 jQuery EasyUI,版本是 1.4.3.x,在使用alert方法的时候如果alert后面执行页面跳转的话alert的消息只会闪一下,就跳到其他页面了 $.messager.a ...

  3. ruby URI类

    一. URI require 'uri' uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413") # ...

  4. ecshop 漏洞如何修复 补丁升级与安全修复详情

    目前ecshop漏洞大面积爆发,包括最新版的ecshop 3.0,ecshop 4.0,ecshop2.7.3全系列版本都存在着高危网站漏洞,导致网站被黑,被篡改,被挂马,许多商城系统深受其漏洞的攻击 ...

  5. 嵌入式框架Zorb Framework搭建四:状态机的实现

    我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...

  6. Json格式化时间

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")@JsonFormat(timezone = "GMT+8", ...

  7. python 推导式的用法

    推导式看了不少,可每次都有新发现 例子1:返回满足条件为真,否则为假 try_list = [1, 2, 3, 4, 5] # 前2种一样, [a > 3 for a in b] [True i ...

  8. Spark是什么

    官方直达电梯 Spark一种基于内存的通用的实时大数据计算框架(作为MapReduce的另一个更优秀的可选的方案) 通用:Spark Core 用于离线计算,Spark SQL 用于交互式查询,Spa ...

  9. nginx+tomcat 反向代理 负载均衡配置

    1.nginx的安装和配置见:http://www.cnblogs.com/ll409546297/p/6795362.html 2.tomcat部署项目到对应的服务器上面并启动,不详解 3.在ngi ...

  10. windows系统下npm升级的正确姿势以及原理

    本文来自网易云社区 作者:陈观喜 网上关于npm升级很多方法多种多样,但是在windows系统下不是每种方法都会正确升级.其中在windows系统下主要的升级方法有以下三种: 首先最暴力的方法删掉no ...