题目

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. PHP菜刀工具WebHandler

    PHP菜刀工具WebHandler   在Web渗透测试中,后台代码如果包含系统命令执行功能,并以用户提交的数据作为参数,就带来潜在的安全隐患.Kali Linux提供一款PHP菜单工具WebHand ...

  2. 懒人的福利?教你用set维护斜率优化凸包

    斜率优化题目大家肯定都做得不少了,有一些题目查询插入点的x坐标和查询斜率都不单调,这样就需要维护动态凸包并二分斜率.(例如bzoj1492) 常规的做法是cdq分治或手写平衡树维护凸包,然而如果我不愿 ...

  3. Code Forces 698A Vacations

    题目描述 Vasya has nn days of vacations! So he decided to improve his IT skills and do sport. Vasya know ...

  4. sigmod2017.org

    http://sigmod2017.org/sigmod-program/#ssession20

  5. linux后台开发核心技术

    3. 常用STL的使用 3.1. string (1)string类的实现(使用strlen.strcpy.strcat.strcmp等,注意判NULL). (2)C++字符串和C字符串的转换:dat ...

  6. How To: Implement a Major Upgrade In Your Installer

    When creating an .msi-based installer, you are strongly encouraged to include logic that supports Wi ...

  7. In-Place upgrade to Team Foundation Server (TFS) 2015 from TFS 2013Team Foundation Server TFS TFS 2015 TFS upgrade TFS with Sharepoint

    This upgrade document gives detailed step by step procedure for the In-Place upgrade from TFS 2013 t ...

  8. ASP.NET MVC与Sql Server建立连接

    用惯了使用Entity Framework连接数据库,本篇就来体验使用SqlConnection连接数据库. 打开Sql Server 2008,创建数据库,创建如下表: create table P ...

  9. Java知识回顾 (2) Java 修饰符

    一.Java 修饰符 1.1 访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java 支持 4 种不同的访问权限. default (即缺省,什么也不写): 在 ...

  10. 【Android病毒分析报告】- 手机支付毒王“银行悍匪”的前世今生

    from://http://blog.csdn.net/androidsecurity/article/details/18984165 2014年1月8日,央视曝光了一款名为“银行悍匪”的手机银行木 ...