[LeetCode]63. Unique Paths II
来源:互联网 发布:淘宝下载ipad版5.1.1 编辑:程序博客网 时间:2024/05/18 02:40
[LeetCode]63. Unique Paths II
题目描述
是 [LeetCode]62. Unique Paths的扩展
增加了路障
思路
动态规划
过程中需要考虑该点是否可以通过,对于不可通过的点,在dp数组中直接置为0;
在初始状态时也要考虑,若终点不可到达,直接置为0,若在与终点处于同一直线的点不可到达,需要将这一点前的所有点置为0
例如
当前状态为
则对应的dp数组为
代码
#include <iostream>#include <vector>using namespace std;class Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int row = obstacleGrid.size(), col = obstacleGrid[0].size(); vector<vector<int>> dp(row, vector<int>(col)); for(int i = row - 1; i >= 0; --i) for (int j = col - 1; j >= 0; --j) { if (i == row - 1 && j == col - 1) dp[i][j] = obstacleGrid[i][j] == 0 ? 1 : 0; else if (i == row - 1) dp[i][j] = dp[i][j + 1] && (!obstacleGrid[i][j]); else if (j == col - 1) dp[i][j] = dp[i + 1][j] && (!obstacleGrid[i][j]); else dp[i][j] = obstacleGrid[i][j] == 0 ? dp[i][j + 1] + dp[i + 1][j] : 0; } return dp[0][0]; }};int main() { vector<vector<int>> obs = { {0,0,0},{0,1,0},{0,0,0} }; Solution s; cout << s.uniquePathsWithObstacles(obs) << endl; system("pause"); return 0;}
0 0
- LeetCode --- 63. Unique Paths II
- LeetCode 63.Unique Paths II
- [leetcode] 63.Unique Paths II
- [leetcode] 63.Unique Paths II
- Leetcode 63. Unique Paths II
- LeetCode 63. Unique Paths II
- [leetcode] 63. Unique Paths II
- leetcode 63. Unique Paths II
- 63. Unique Paths II LeetCode
- LeetCode 63. Unique Paths II
- [LeetCode]63. Unique Paths II
- leetcode 63. Unique Paths II
- LeetCode *** 63. Unique Paths II
- leetcode 63. Unique Paths II
- Leetcode:63. Unique Paths II
- LeetCode 63. Unique Paths II
- leetcode 63.Unique Paths II
- LeetCode 63. Unique Paths II
- 机器学习基础——RandomForest
- vim操作
- 数学常数e的含义
- 《C和指针》第一章课后
- day84_babasport第十天
- [LeetCode]63. Unique Paths II
- Thrift深入理解
- 微信小程序——Error之util.randomString is not a function
- MySQL调优
- c语言中的左结合右结合
- leetcode--485. Max Consecutive Ones
- 网络工程师考试复习心得(附详细电子笔记)
- java邮件开发--电子邮件基础
- YUV格式解析