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/
随机推荐
- 【wqs二分】HHHOJ#15. 赤
这个wqs二分并不熟练…… 题目描述 #15. 赤 题目分析 两维都用wqs二分,其他没有什么特殊之处. 重点在于,wqs二分还原最优解的时候,增量是强制给的k. #include<bits/s ...
- Linux虚拟机里用X11协议打开图形界面的eclipse
1.下载工具包 XLaunch(安装到win)https://xming.en.softonic.com/ Eclipse IDE for C/C++ Developers(虚拟机里解压到 /data ...
- 通过composer安装阿里大于接口扩展
# 安装laravel阿里大鱼服务 composer require iscms/alisms-for-laravel laravel配置 # 注册服务 # 在config\app.php文件中找到P ...
- Struts2和Spring MVC 区别 今天面试被问到了
虽然说没有系统的学习过Spring MVC框架, 但是工作这么长时间, 基本上在WEB层使用的都是Spring MVC, 自己觉得Struts2也是一个不错的WEB层框架, 这两种框架至今自己还未有比 ...
- CodeForce--Benches
A. Benches There are nn benches in the Berland Central park. It is known that aiai people are curr ...
- linux学习-主机的细部权限规划:ACL 的使用
传统的权限仅有三种身份 (owner, group, others) 搭配三种权限 (r,w,x) 而已,并没有办法单纯的针对某一个使用者或某一个群 组来设定特定的权限需求,此时就得要使用 ACL 这 ...
- 算法学习记录-查找——折半查找(Binary Search)
以前有个游戏,一方写一个数字,另一方猜这个数字.比如0-100内一个数字,看谁猜中用的次数少. 这个里面用折半思想猜会大大减少次数. 步骤:(加入数字为9) 1.因为数字的范围是0-100,所以第一次 ...
- hdu 1011 Starship Troopers(树形背包)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- angularjs报错问题记录
1.[$injector:unpr]:没有找到注入的东西 2.$compile:multidir:多指令编译错误. 3.[ng:areq]:重复定义了ng-controller. 4 ...
- const用法归纳总结 C++
非常好的一篇分析const的总结归纳, 在此谢谢原作者:http://blog.csdn.net/zcf1002797280/article/details/7816977 在普通的非 const成员 ...