【题解】【矩阵】【回溯】【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 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的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- [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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
随机推荐
- [转]连续创建多个Oracle触发器失败,单个创建才成功的解决方法
连续创建多个Oracle触发器失败,单个创建才成功的解决方法 1.当我连续执行创建多个触发器时,总是报编译通过,但存在警告或错误.如下: create or replace trigger t ...
- java8Lambda详解
[移步] http://blog.csdn.net/ioriogami/article/details/12782141/
- [Js]滑动门效果
描述:鼠标移动到一副图片上,会显示该副图片的全貌,而其他图片会显示概貌 一.没有动画效果的运动 思路: 1.定好每张图片的初始位置(第一张完全显示,234只露出一部分) 2.计算每道门的移动距离(即未 ...
- 在JavaScript 自定义对象来模拟Java中的Map
直接看代码: //模拟一个Map对象 function Map(){ //声明一个容器 var container={}; //定义一个put方法,向容器中存值 this.put=function(k ...
- 使用ASP.Net WebAPI构建REST服务(五)——客户端
WebAPI是标准的Http协议,支持Http协议的客户端(如浏览器)都可以访问.但是,有的时候我们如果想在自己的程序中使用WebAPI时,此时就要实现自己的客户端了.我之前介绍过在.Net 4.5中 ...
- svn自动更新
果对svn不熟悉,当svn上面有更新时,想看到实时效果,就得去web目录手动更新,比较麻烦 其它svn有一个自动更新的功能 利用 hook 在svn 仓库目录下面有一个hook目录 在post-c ...
- 第一次使用Git心得体会
用书本上的概念讲,Git是一个分布式的版本控制工具,每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,能够不依赖于网络和中心服务器.也就是说Git能够不需要服务器而在 ...
- 支持多人协作的在线免费作图工具:ProcessOn
之前朋友给我推荐一款作图工具ProcessOn,出于好奇我就研究了一下它,今天我就给大家简单介绍一下这款免费的在线作图工具:ProcessOn 首先使用ProcessOn我们需要有一个帐号,这样每次操 ...
- 一道面试题,简单模拟spring ioc
自己实现的,程序写的土了点,很多情况没去考虑,主要是复习理解怎么使用反射来实现spring 的依赖注入. package dom4jtest; import java.lang.reflect.Inv ...
- win8系统 host文件无法修改解决之道
host文件,路径为:C:\windows\system32\drivers\etc\hosts 方法/步骤: 方法1:用notepad++打开host文件,修改和保存 方法2:(1)首先用管理管权限 ...