LeetCode OJ--Unique Paths II **
https://oj.leetcode.com/problems/unique-paths-ii/
图的深搜,有障碍物,有的路径不通。
刚开始想的时候用组合数算,但是公式没有推导出来。
于是用了深搜,递归调用。
但是图最大是100*100,太大了,超时。考虑到在计算(2,1)和(1,2)都用到了(2,2),也就是说有了重复计算。于是记录这些中间的数据。
而有的地方因为有障碍物,所以得的路径值是0,这又要和没有计算做个区分,于是又加了些数据,标志是否已经计算过了。
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(obstacleGrid.size() == )
return ;
int row = obstacleGrid.size();
int col = obstacleGrid[].size();
//the destination position unavailable
if(obstacleGrid[row-][col-] == || obstacleGrid[][] ==)
return ;
vector<vector<bool> > handled; //to mark if xy has handled
vector<vector<int> > tempRecord;
handled.resize(row);
tempRecord.resize(row);
for(int i = ;i<row;i++)
{
tempRecord[i].resize(col);
handled[i].resize(col);
for(int j = ;j<col;j++)
handled[i][j] = false; // all initialized false
}
int ans = calcPath(obstacleGrid,,,row,col,tempRecord,handled);
//no path
if(ans < )
ans = ;
return ans;
}
int calcPath(vector<vector<int> > &grid ,int xPos, int yPos,int row,int col,vector<vector<int> > &tempRecord,vector<vector<bool> > &handled)
{
//destination
if(xPos == row - && yPos == col - )
return ;
//unhandle this position
if(tempRecord[xPos][yPos] == && handled[xPos][yPos] == false)
{
int ans = ;
if(xPos < row- && grid[xPos + ][yPos] ==)
if(handled[xPos+][yPos] == false)
ans = calcPath(grid,xPos+,yPos,row,col,tempRecord,handled);
else
ans = tempRecord[xPos+][yPos];
if(yPos < col - && grid[xPos][yPos+] == )
//unhandled
if(handled[xPos][yPos+] == false)
ans += calcPath(grid,xPos,yPos+,row,col,tempRecord,handled);
else
ans += tempRecord[xPos][yPos+];
tempRecord[xPos][yPos] = ans;
}
handled[xPos][yPos] = true;
return tempRecord[xPos][yPos];
}
};
LeetCode OJ--Unique Paths II **的更多相关文章
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 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). ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- [LeetCode] 63. 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 ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【题解】【矩阵】【回溯】【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 ...
- [LeetCode][Java] Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
随机推荐
- 【动态规划】bzoj1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
思路自然的巧妙dp Description 有N头奶牛,每头那牛都有一个标号Pi,1 <= Pi <= M <= N <= 40000.现在Farmer John要把这些奶牛分 ...
- VIM 编辑器 -使用详解记录
1.什么是 vim? Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用.简单的来说, vi 是老式的字处理器,不过功能已经很齐全了,但 ...
- 【java】类成员的访问限制关系
- Python基础(六)——面向对象编程
(1)定义类和实例 这一部分难得和 Java 较为一致,直接写个例子: class Stu: def __init__(self, name, id): # 构造方法 self.name = name ...
- pyecharts用法,本人亲测,陆续更新
主题 除了默认的白色底色和dark之外,还支持安装扩展包 pip install echarts-themes-pypkg echarts-themes-pypkg 提供了 vintage, maca ...
- FIFO buffer 和普通buffer区别
1.FIFO可以说一块具体的硬件存储设备,也可以说程序在内存中开辟的一段内存区域.而buffer往往就是一段缓冲的数据区域 2.FIFO的数据是先进先出的,而buffer没有这个限制,可以全局访问 3 ...
- Docker容器技术的核心原理
目录 1 前言 2 docker容器技术 2.1 隔离:Namespace 2.2 限制:Cgroup 2.3 rootfs 2.4 镜像分层 3 docker容器与虚拟机的对比 1 前言 上图是百度 ...
- 创建dll及使用
一.创建动态链接库文件 ** 1.打开VS2013,选择文件,新建工程 2.选择新建W32控制台应用程序,这里将工程名改为makeDLL 3.在应用程序类型中选择DLL,点击完成 4.完成以上步 ...
- clr(Windows 运行时和公共语言运行时)
Windows 运行时 编译器使用 COM 引用计数机制来确定对象是否不再使用并可以删除. 因为从 Windows 运行时接口派生的对象实际上是 COM 对象,所以这是可行的. 在创建或复制对象时 ...
- 并查集 - BZOJ 1104 [POI2007]洪水
BZOJ 1104 [POI2007]洪水 描述 AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了他的所有顾问(包括你 ...