题目

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.

[
[0,0,0],
[0,1,0],
[0,0,0]
]

The total number of unique paths is 2.

Note: m and n will be at most 100.

题解:

这道题大体想法跟Unique Path是一样的。

只是要单独考虑下障碍物对整个棋盘的影响。

先看看初始条件会不会受到障碍物的影响。

假设整个棋盘只有一行,那么在第i个位置上设置一个障碍物后,说明位置i到最后一个格子这些路都没法走了。

如果整个棋盘只有一列,那么第i位置上的障碍物,也会影响从第i位置往后的路。

所以说明,在初始条件时,如果一旦遇到障碍物,障碍物后面所有格子的走法都是0。

再看求解过程,当然按照上一题的分析dp[i][j] = dp[i-1][j] + dp[i][j-1] 的递推式依然成立(机器人只能向下或者向右走嘛)。但是,一旦碰到了障碍物,那么这时的到这里的走法应该设为0,因为机器人只能向下走或者向右走,所以到这个点就无法通过。

处理完障碍物的特殊问题,依照unique paths改一下代码就好。

代码如下:

 1    public int uniquePathsWithObstacles(int[][] obstacleGrid) {  
 2         int m = obstacleGrid.length;  
 3         int n = obstacleGrid[0].length;  
 4         
 5         if(m==0||n == 0)  
 6             return 0; 
 7         
 8         if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1)  
 9             return 0; 
             
         int[][] dp = new int[m][n]; 
         
         dp[0][0] = 1;  
         for(int i = 1; i < n; i++){  
             if(obstacleGrid[0][i] == 1)  
                 dp[0][i] = 0;  
             else 
                 dp[0][i] = dp[0][i-1];  
         }  
         
         for(int i = 1; i < m; i++){  
             if(obstacleGrid[i][0] == 1)  
                 dp[i][0] = 0;  
             else 
                 dp[i][0] = dp[i-1][0];  
         }  
         
         for(int i = 1; i < m; i++){  
             for(int j = 1; j < n; j++){  
                 if(obstacleGrid[i][j] == 1)  
                     dp[i][j] = 0;  
                 else  
                     dp[i][j] = dp[i][j-1] + dp[i-1][j];  
             }  
         }  
         return dp[m-1][n-1];  
     } 

Unique Paths II leetcode java的更多相关文章

  1. Unique Paths II [LeetCode]

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

  2. Unique Paths II ——LeetCode

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

  3. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  4. LeetCode: Unique Paths II 解题报告

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

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

  6. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  7. [Leetcode Week12]Unique Paths II

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

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

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

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

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

随机推荐

  1. CentOS安装openvpn报错:error: route utility is required but missing

    centos7特有,直接安装net-tools即可. 参考: https://forums.openvpn.net/viewtopic.php?t=21432

  2. 在eclipse中查看Android源码

    声明:高手跳过此文章 当我们在eclipse中开发android程序的时候.往往须要看源码(可能是出于好奇,可能是读源码习惯),那么怎样查看Android源码呢? 比方以下这样的情况 图1 如果我们想 ...

  3. LPCScrypt, DFUSec : USB FLASH download, programming, and security tool, LPC-Link 2 Configuration tool, Firmware Programming

    What does this tool do? The LPC18xx/43xx DFUSec utility is a Windows PC tool that provides support f ...

  4. 新玩的windows phone app studio

    其实我是一直想开发windows phone 8平台的应用的,奈何开始windows phone 8开发却是不是件容易的事.Windows phone 8的开发其实是对计算机的硬件有要求的,首先要装w ...

  5. tms web core介绍

    tms web core介绍 TMS Web CORE是基于将Delphi UI代码编译为javascript并以此方式创建的 称为单页应用程序.TMS Web核心应用程序可以包含多个表单. 这些多个 ...

  6. Android 和 iOS 应用程序开发对比 [持续更新]

    1.Android 用字典模式统一管理应用程序中UI上用到的所有字符串. 比如文本框的默认文本.按钮的名字等等.表现形式:XML文件 Android中 "@string/text_filed ...

  7. 一文犀利看懂中美贸易战 z

    如今的中国面对着前所未有的经济全球化的大环境.面对着如何成为创新性国家的重任. 钛媒体注:美国东部时间 7 月 6 日凌晨0:01 分,美国正式开始对 340 亿美元的中国产品加征 25% 的关税,这 ...

  8. C#泛型委托Predicate、Action、Func

    Predicate Predicate泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素.通过查看源码发现 Pr ...

  9. Asp.Net Mvc3.0(MEF依赖注入实例)

    前言 在http://www.cnblogs.com/aehyok/p/3386650.html前面一节主要是对MEF进行简单的介绍.本节主要来介绍如何在Asp.Net Mvc3.0中使用MEF. 准 ...

  10. android之Android中的SQL查询语句LIKE绑定参数问题解决办法(sqlite数据库)

    由于考虑到数据库的安全性,不被轻易SQL注入,执行查询语句时,一般不使用直接拼接的语句,而是使用参数传递的方法.然后在使用参数传递的方法中时,发现当使用like方式查询数据时,很容易出现一个问题. 错 ...