Unique Paths II

Total Accepted: 22828 Total Submissions: 81414My Submissions

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.

把存在Obstacle的位置标记为-1,表示无法通行
 
 
  1. class Solution {
  2. public:
  3. int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
  4. int m=obstacleGrid.size();
  5. int n=obstacleGrid[].size();
  6.  
  7. int dp[][];
  8.  
  9. dp[][]=obstacleGrid[][]==?-:;
  10.  
  11. for(int i=;i<m;i++)
  12. {
  13. if(obstacleGrid[i][]==)
  14. {
  15. dp[i][]=-;
  16. continue;
  17. }
  18. if(dp[i-][]==-) dp[i][]=-;
  19. else dp[i][]=;
  20. }
  21.  
  22. for(int j=;j<n;j++)
  23. {
  24. if(obstacleGrid[][j]==)
  25. {
  26. dp[][j]=-;
  27. continue;
  28. }
  29.  
  30. if(dp[][j-]==-) dp[][j]=-;
  31. else dp[][j]=;
  32. }
  33.  
  34. for(int i=;i<m;i++)
  35. {
  36. for(int j=;j<n;j++)
  37. {
  38. if(obstacleGrid[i][j]==)
  39. {
  40. dp[i][j]=-;
  41. continue;
  42. }
  43. if(dp[i-][j]==-&&dp[i][j-]==-) dp[i][j]=-;
  44. else if(dp[i-][j]==-&&dp[i][j-]!=-) dp[i][j]=dp[i][j-];
  45. else if(dp[i-][j]!=-&&dp[i][j-]==-) dp[i][j]=dp[i-][j];
  46. else if(dp[i-][j]!=-&&dp[i][j-]!=-) dp[i][j]=dp[i-][j]+dp[i][j-];
  47. }
  48. }
  49.  
  50. return dp[m-][n-]==-?:dp[m-][n-];
  51. }
  52. };
 
 
 
实际上不用标记也可以,dp[i][j]=0就表示了没有路
 
  1. class Solution {
  2. public:
  3. int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
  4. int m=obstacleGrid.size();
  5. int n=obstacleGrid[].size();
  6.  
  7. int dp[][];
  8. //vector<vector<int>> dp(m,vector<int>(n));
  9.  
  10. dp[][]=obstacleGrid[][]==?:;
  11. for(int i=;i<m;i++)
  12. {
  13. dp[i][]=obstacleGrid[i][]==?:dp[i-][];
  14. }
  15. for(int j=;j<n;j++)
  16. {
  17. dp[][j]=obstacleGrid[][j]==?:dp[][j-];
  18. }
  19. for(int i=;i<m;i++)
  20. {
  21. for(int j=;j<n;j++)
  22. {
  23. dp[i][j]=obstacleGrid[i][j]==?:dp[i-][j]+dp[i][j-];
  24. }
  25. }
  26.  
  27. return dp[m-][n-];
  28. }
  29. };

【leetcode】Unique Paths II的更多相关文章

  1. 【题解】【矩阵】【回溯】【Leetcode】Unique Paths II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. 【Leetcode】【Medium】Unique Paths II

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

  3. 【题解】【排列组合】【素数】【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 ...

  4. 【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 ...

  5. 【数组】Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  6. 【LeetCode练习题】Unique Paths II

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

  7. [Leetcode Week12]Unique Paths II

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

  8. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

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

随机推荐

  1. 大学站防SQL注入代码(ASP版)

    方法1: Replace过滤字符 解决方法:查找login.asp下的<from找到下边的类似username=request.Form(”name”) pass=request.Form(”p ...

  2. F5负载均衡的初识和基本配置

    目前全球范围内应用比较广泛的负载均衡设备为美国的F5.F5于2000年底进驻中国,在国内业界,F5负载均衡产品已经成为了主流负载均衡技术的代名词.下面我们对F5负载均衡设备做一个基本介绍,方便大家去认 ...

  3. linux下的chm阅读器?

    pre和code标签是可以同时使用的, 通常pre放在code的前面. 由于 code, pre中不能使用 换行, 段落, 和 尖括号标签, 所以, 对于尖括号, 要换成 html的 实体符号 < ...

  4. css使 同一行内的 文字和图片 垂直居中对齐?

    设置父容器, 使 父容器 div 下的所有元素 都 垂直对齐: father-container *{ vertical-align:middle 找回密码

  5. 解决前面有一篇文章中'flashplayer.so为什么要设置777权限的'问题 的 思考了

    列出某个目录下的所有内容? ls -A, -A等同于-a, 即是-all, 只是-A 不显示.和.. ll ls 某个目录, 如果它下面没有任何东西, 那么 就没有输出! 同时, ll某个目录, 不会 ...

  6. 导出Excel之Epplus使用教程1(基本介绍)

    1.前言 目前Epplus的介绍中文资料很少,我也一直在摸索中使用它,以下是我在使用过程中得到的经验,写出来供大家参考.本系列共4章: 导出Excel之Epplus使用教程1(基本介绍) 导出Exce ...

  7. 【转】CSS3动画帧数科学计算法

    本文来源于:财付通TID 原作者:bboy90 总结都浓缩在这个工具里了,想知道工具的地址或想窥探工具诞生的趣事请往下看 . —————————————————————–     华丽丽的开篇     ...

  8. VPN添加静态路由表(指定程序或资源走VPN)

    在某此情况下,我们希望为VPN客户端指定线路,比如只有公司的资源或网站才使用VPN连接,其它的网络请求依然走他们自己的默认网关. 这种情况下,我们就需要给VPN客户端添加静态路由规则并取消VPN连接的 ...

  9. Office 2010/2007 简繁体转换按钮不见了?

    注:此文章来自微软官方,原文链接:http://support.microsoft.com/kb/2459493/zh-tw 经测试可解决问题.——————————– 通常发生这样的问题,是由于繁简转 ...

  10. StarWind的安装配置

    将安装包拷贝到本地后,执行以下exe文件,默认下一步,直到安装完成. 需汉化版本则将language_chinese.xml文件拷贝到以下路径: C:\Program Files\StarWind S ...