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

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

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.

思路:

很典型的回溯问题,到[i,j]的路径等于到[i-1,j]和[i,j-1]的路径之和,然后为了避免重复求解子问题,结合查表法记录子问题结果。编码要点在于处理 trivial case。

代码:

 int uniquePaths2Node(vector<vector<int> > &oGrid, vector<vector<int> > &paths, int i, int j){
//if(i < 0 || j < 0) return 0;//failed [[1]]=>0, 应该把控制条件放下面,不给调用不valid的子问题
if(oGrid[i][j] == ) return ; if(i == && j == ) return ^ oGrid[][];//failed [[0]]=>1 int P = ;
if(i > && oGrid[i-][j] != ){
if(paths[i-][j] == -)
paths[i-][j] = uniquePaths2Node(oGrid, paths, i-, j);
P += paths[i-][j];
}
if(j > && oGrid[i][j-] != ){
if(paths[i][j-] == -)
paths[i][j-] = uniquePaths2Node(oGrid, paths, i, j-);
P += paths[i][j-];
}
return P;
}
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m = obstacleGrid.size();
if(m == ) return ;
int n = obstacleGrid[].size();
if(n == ) return ; vector<vector<int> > paths(m, vector<int>(n, -));
return uniquePaths2Node(obstacleGrid, paths, m-, n-);
}

【题解】【矩阵】【回溯】【Leetcode】Unique Paths II的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

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

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

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

  3. LEETCODE —— Unique Paths II [Dynamic Programming]

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

  4. [LeetCode] Unique Paths II 不同的路径之二

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

  5. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. Leetcode Unique Paths II

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

  7. [Leetcode] unique paths ii 独特路径

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

  8. [Leetcode Week12]Unique Paths II

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

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

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

随机推荐

  1. eclipse-mysql-tomcat bug之旅

    赶紧默念三遍google大法好... [连接数据库 servlet调用提示找不到可加载的driver,普通的.java文件没问题] 表示不服啊...明明可以连上啊...为什么多了几个中间界面就不好使了 ...

  2. 用于主题检测的临时日志(452a49c2-4455-430f-a1cc-bbcd2d1944dd - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(95c74eab-5822-4f4b-b0e5-009feb9cae8d - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  3. 发送有序广播Ordered Broadcast

    import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.vi ...

  4. thinking in java之Collections工具类的使用

    代码摘自<thinking in java>4td 此实例非常好的总结了Collections的一些常见方法的使用. package countainers; import java.ut ...

  5. JAVA与指针

    首先,提个问题:JAVA中没有指针,JAVA中有指针,哪个一个对呢? 答:都对,JAVA中没有指针,因为我们不能对指针直接操作,像C++那样用->来访问变量. JAVA有指针,因为JDK中封装了 ...

  6. CSU 1021 B(Contest #3)

    Description 从m个不同元素中取出n (n ≤ m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数.组合数的计算公式如下: C(m, n) = m!/((m - n)!n! ...

  7. mouseOver与rollOver

    区别: 当父容器监听这两个事件,鼠标从父容器移到子容器再移回父容器时,会触发mouseOver.mouseout事件,但是不会触发rollover.rollout事件.

  8. SQL语句查询所耗时间与效能的语句

    1)SQL查询所耗时间语句 原理:记录当前时间1,执行SQL语句,记录当前时间2,显示时间2与时间1的差. 由于第一次执行的所耗时间为真实时间,之后会保存在缓存中,所以第二次之后的查询所耗时间都会比第 ...

  9. iPhone 6/6 Plus国行版开卖当日抢购攻略

    在距离苹果首批发售时隔一个月也就是北京时间10月17日,苹果iPhone 6.iPhone 6 Plus终于也要在中国大陆开卖,众多国内用户终于有机会安排自己的购机计划.据不完全数据显示,目前iPho ...

  10. Date的转换

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String str = sdf.format(一个date对 ...