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.

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

把存在Obstacle的位置标记为-1,表示无法通行
 
 
 class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m=obstacleGrid.size();
int n=obstacleGrid[].size(); int dp[][]; dp[][]=obstacleGrid[][]==?-:; for(int i=;i<m;i++)
{
if(obstacleGrid[i][]==)
{
dp[i][]=-;
continue;
}
if(dp[i-][]==-) dp[i][]=-;
else dp[i][]=;
} for(int j=;j<n;j++)
{
if(obstacleGrid[][j]==)
{
dp[][j]=-;
continue;
} if(dp[][j-]==-) dp[][j]=-;
else dp[][j]=;
} for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
if(obstacleGrid[i][j]==)
{
dp[i][j]=-;
continue;
}
if(dp[i-][j]==-&&dp[i][j-]==-) dp[i][j]=-;
else if(dp[i-][j]==-&&dp[i][j-]!=-) dp[i][j]=dp[i][j-];
else if(dp[i-][j]!=-&&dp[i][j-]==-) dp[i][j]=dp[i-][j];
else if(dp[i-][j]!=-&&dp[i][j-]!=-) dp[i][j]=dp[i-][j]+dp[i][j-];
}
} return dp[m-][n-]==-?:dp[m-][n-];
}
};
 
 
 
实际上不用标记也可以,dp[i][j]=0就表示了没有路
 
 class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m=obstacleGrid.size();
int n=obstacleGrid[].size(); int dp[][];
//vector<vector<int>> dp(m,vector<int>(n)); dp[][]=obstacleGrid[][]==?:;
for(int i=;i<m;i++)
{
dp[i][]=obstacleGrid[i][]==?:dp[i-][];
}
for(int j=;j<n;j++)
{
dp[][j]=obstacleGrid[][j]==?:dp[][j-];
}
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
dp[i][j]=obstacleGrid[i][j]==?:dp[i-][j]+dp[i][j-];
}
} return dp[m-][n-];
}
};

【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. spring 容器技术入门

    官方文档 翻译 https://waylau.gitbooks.io/spring-framework-4-reference/content/III.%20Core%20Technologies/C ...

  2. iOS-马上着手开发iOS应用应用程序-第一部分介绍

    教程:基础 main 中的 main.m 函数会调用自动释放池 (autorelease pool) 中的 UIApplicationMain 函数. @autoreleasepool { retur ...

  3. Yii2 rules验证规则

    Rules验证规则:  required : 必须值验证属性||CRequiredValidator 的别名, 确保了特性不为空. [['字段名1','字段名2'],required]    //字段 ...

  4. JavaScript正则表达式方法总结

    str.match(reg) 1.reg没有全局标志g,match将只执行一次匹配.匹配成功返回一个数组,arr = [$0,$1,$2,...,index,str],匹配失败返回null. arr中 ...

  5. MySQL里的found_row()与row_count()的解释及用法

    MySQL中有两个函数来计算上一条语句影响了多少行,不同于SqlServer/Oracle,不要因为此方面的差异而引起功能问题   出处:mysqlpub.com MySQL中有两个函数来计算上一条语 ...

  6. Linux下中文字符乱码的问题

    来源:Linux社区  作者:frankfellow Linux下中文经常会出现乱码,有的是浏览网页出现乱码:有的是文本模式下显示中文出现乱码.下图显示的是我遇到的问题.我安装的是CentOS,x-w ...

  7. Java字节流:FilterInputStream FilterOutputStream

    ----------------------------------------------------------------------------------- FilterInputStrea ...

  8. hdu4950 Monster (水题)

    4950 Monster Monster Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...

  9. ThinkPHP3.2.3扩展之生成PDF文件(MPDF)

    目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendo ...

  10. js 日期处理,json处理

    模块化js :requirejshttp://www.requirejs.cn/ 好用的日期控件:http://www.bootcss.com/p/bootstrap-datetimepicker/i ...