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/
随机推荐
- Vue木桶布局插件
公司最近在重构,使用的是Vue框架.涉及到一个品牌的布局,因为品牌的字符长度不一致,所以导致每一个的品牌标签长短不一.多行布局下就会导致每行的品牌布局参差不齐,严重影响美观.于是就有了本篇的木 ...
- 用Python抓取并分析了1982场英雄联盟数据,教你开局前预测游戏对局胜负!
英雄联盟想必大多数读者不会陌生,这是一款来自拳头,由腾讯代理的大型网络游戏,现在一进网吧,你就能发现一大片玩英雄联盟的人.在2017年中国战队无缘鸟巢的世界总决赛后,一大片人选择了弃游,只是终究没躲过 ...
- leetcode-8-pointer
void deleteNode(ListNode* node) { *node = *node->next; }
- #1 add life to static pages && connect to MySQL
由于实验室 Project 中需要用到PHP, 之前也没接触过 PHP, 因此把 编程入门 <Head Fist PHP & MySQL >找来花了四五天快速过了一遍. 现在想把书 ...
- P3369 【模板】普通平衡树FHQtreap
P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...
- LA 7049 Galaxy 枚举
题意: \(x\)轴上有\(n\)个质量为\(1\)的点,他们的坐标分别为\(x_i\). 质心的坐标为\(\frac{\sum{x_i}} {n}\) 转动惯量为\(\sum{d_i^2}\),其中 ...
- luogu1972 [SDOI2009]HH的项链
莫队裸题还不带修改 #include <algorithm> #include <iostream> #include <cstdio> #include < ...
- GBDT 与 XGBoost
GBDT & XGBoost ### 回归树 单棵回归树可以表示成如下的数学形式 \[ f(x) = \sum_j^Tw_j\mathbf{I}(x\in R_j) \] 其中\(T\)为叶节 ...
- 【Mysql 优化 6】mysql优化的内容和思路
根据最近做mysql优化,以及参照的官方文档的一些知识点,总结一下,如何下手去优化mysql 数据库.PS:更多可能是我个人的笔记总结记录,仅供参考 一.优化的内容 可以优化的内容,从范围的大小,可以 ...
- WebSocket & websockets
WebSocket & websockets https://en.wikipedia.org/wiki/WebSocket https://developer.mozilla.org/en- ...