Unique Paths II
来源:互联网 发布:mac r语言安装 编辑:程序博客网 时间:2024/04/30 20:28
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[ [0,0,0], [0,1,0], [0,0,0]]
The total number of unique paths is 2.
Note: m and n will be at most 100.
和第一题类似,dp方程还是f[i][j] = f[i-1][j] + f[i][j-1]; 只不过因为有障碍物的原因,碰到障碍物需要重置为0。
所以修改dp方程为f[i][j] = obstacleGrid[i][j] == 1 ? 0 : f[i-1][j] + f[i][j-1];
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid){ int m=obstacleGrid.size(); int n=obstacleGrid[0].size(); int f[m][n]; f[0][0] = obstacleGrid[0][0] == 1 ? 0 : 1; for(int i = 1; i < m; i++) f[i][0] = obstacleGrid[i][0] == 1 ? 0 : f[i-1][0]; for(int i = 1; i < n; i++) f[0][i] = obstacleGrid[0][i] == 1 ? 0 : f[0][i-1]; for(int i = 1; i < m; i++) for(int j = 1; j < n; j++) f[i][j] = obstacleGrid[i][j] == 1 ? 0 : f[i-1][j] + f[i][j-1]; return f[m-1][n-1];}
0 0
- Unique Paths & Unique Paths II
- Unique Paths &&Unique Paths II
- Unique Paths & Unique Paths II
- Unique Paths & Unique Paths II
- LeetCode: Unique Paths II
- [Leetcode] Unique Paths II
- LeetCode : Unique Paths II
- Unique Paths II
- [LeetCode] Unique Paths II
- unique paths II
- Unique Paths II
- [LeetCode]Unique Paths II
- Unique Paths II
- Unique Paths II
- Unique Paths II
- Unique Paths II
- [leetcode]Unique Paths II
- Unique Paths II
- codeforces 148D之概率DP
- 优秀的人们每天会做的10件事
- C#设计模式之命令
- 【中断异常】时钟中断
- 11 Insanely Great iOS Developers Sites
- Unique Paths II
- 理解EXCEL中日期和时间的序列号
- Dp-剑客决斗
- IOS基础进阶-牛刀小试 UIViewController UIView
- css笔记(二)——几种常用的模式
- 百度站长页面优化建议
- 6. 函数
- 初学菜鸟的学习日记——栈结构(JAVA)
- 归并排序