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.

  1. [
  2. [0,0,0],
  3. [0,1,0],
  4. [0,0,0]
  5. ]

The total number of unique paths is 2.

Note: m and n will be at most 100.

代码:

  1. class Solution {
  2. public:
  3. int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
  4. if(obstacleGrid.size()==) return ;
  5. int row=obstacleGrid.size();
  6. int col=obstacleGrid[].size();
  7. int dp[][];
  8. memset(dp,,sizeof(dp));
  9. bool rblockTag=false;
  10. bool cblockTag=false;
  11. if(obstacleGrid[][]==||obstacleGrid[row-][col-]==)
  12. return ;
  13. for(int i=;i<col;++i){
  14. if(obstacleGrid[][i]==) rblockTag=true;
  15. if(!rblockTag) dp[][i]=;
  16. else dp[][i]=;
  17. }
  18. for(int j=;j<row;++j){
  19. if(obstacleGrid[j][]==) cblockTag=true;
  20. if(!cblockTag) dp[j][]=;
  21. else dp[j][]=;
  22. }
  23. for(int i=;i<row;++i){
  24. for(int j=;j<col;++j){
  25. if(obstacleGrid[i][j]==) dp[i][j]=;
  26. else dp[i][j]=dp[i][j-]+dp[i-][j];//i和j是从1开始
  27. }
  28. }
  29. return dp[row-][col-];
  30. }
  31. };

Unique Paths II (dp题)的更多相关文章

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

  2. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

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

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

  4. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  5. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  6. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  7. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  8. 【LeetCode练习题】Unique Paths II

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

  9. 【LeetCode】63. Unique Paths II

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

  10. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

随机推荐

  1. HttpServletResponse 的状态码

    public static final int  SC_ACCEPTED  202 public static final int  SC_BAD_GATEWAY  502 public static ...

  2. 关于bin和obj文件夹。debug 和release的区别(转)

    关于bin和obj文件夹. 楼主hcaihao(影子男孩)2002-05-29 20:04:24 在 .NET技术 / C# 提问 VS.Net会生成bin和obj文件夹以及它们下面的Debug和Re ...

  3. Java JDK装配置

     1- 介绍 本文章介绍JAVA开发环境安装是基于:  Java8(JDK8) 2- 下载JDK http://www.oracle.com/technetwork/java/javase/dow ...

  4. COGS 942. [東方S3] 比那名居天子

    Problem 1 比那名居天子(tenshi.cpp/c/pas) 题目描述 在幻想乡,比那名居天子是管理着『要石』的天人.『要石』是能够引发和镇压地震的存在,当然也可以用来改变地形.因为在幻想乡引 ...

  5. 朴素贝叶斯法(naive Bayes)

    <统计学习方法>(第二版)第4章 4 朴素贝叶斯法 生成模型 4.1 学习与分类 基于特征条件独立假设学习输入输出的联合概率分布 基于联合概率分布,利用贝叶斯定理求出后验概率最大的输出 条 ...

  6. ROI(Region of Interesting)

    ROI(region of interest),感兴趣区域.机器视觉.图像处理中,从被处理的图像以方框.圆.椭圆.不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI.在Halcon.Ope ...

  7. <c:forEach>实例演示

    <c:forEach>实例演示 <%@ page language="java" contentType="text/html; charset=UTF ...

  8. linux 安装nginx 集成emq

    1:下载nginx-1.12.2.tar.gz http://nginx.org/en/download.html 2:解压 tar -zxvf  nginx-1.12.2.tar.gz 3:进行co ...

  9. 前段开发 react native tab功能

    import React, { Component } from 'react'; import { StyleSheet, Text, View, Image, } from 'react-nati ...

  10. 字典(trie)树--从入门到入土

    今天再来认识一个强大的数据结构. 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词 ...