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/
随机推荐
- ubuntu安装easygui模块
使用pip安装easygui 如果未安装pip,则使用如下命令 sudo apt-get install python-pip 安装完pip后,使用如下命令安装easygui sudo pip ins ...
- tomcat如何登录Server Status、Manager App、Host Manager
启动tomcat后,访问127.0.0.1会进入如下页面 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址:https://www.cnblogs.com/poterliu/p/9602 ...
- vue 顶级组件
快 有时候懒的把一些通用组件写到template里面去,而业务中又需要用到,比如表示loading状态这样组件. 如果是这样的组件,可以选择把组件手动初始化,让组件在整个app生命周期中始终保持活跃. ...
- 【laravel】Laravel 5 TokenMismatchException on PHP 5.6.9
When I realized this was only happening in IE and Chrome, but not Firefox, it led me to the fix. The ...
- GoF23种设计模式之结构型模式之外观模式
一.概述 为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 二.适用性 1.当你要为一个复杂子系统提供一个简单接口的时候.子系统 ...
- manjaro中virtualbox(vbox)配置
一.更新源的配置: 1).自动方法: 在 终端 执行下面的arch" style="color: #002be5">命令从官方的源列表中对中国源进行测速和设置 su ...
- leetcode-11-dfs
DFS算法: explore(G, v) visited(v) = trueprevisit(v) for each edge(v, u) in E: if not visited(u): explo ...
- (转)rvm安装与常用命令
rvm是一个命令行工具,可以提供一个便捷的多版本ruby环境的管理和切换. https://rvm.io/ 如果你打算学习ruby/rails, rvm是必不可少的工具之一. 这里所有的命令都是再用户 ...
- 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)
An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- python单例模式的几种实现方法
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...