leetcode_63题——Unique Paths II(动态规划)
2015-06-02 15:46
357 查看
Unique Paths II
Total Accepted: 35061 Total Submissions: 125276My SubmissionsQuestion Solution
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
1and
0respectively 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.
Hide Tags
Array Dynamic Programming
这道题只是在前面62题的基础上加了路障,也很简单,就是在路障这个地方就把它的路径的个数直接设为0
就好了,其它的和62题没啥区别
#include<iostream> #include <vector> using namespace std; int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { if(obstacleGrid.empty()) return 0; int m=obstacleGrid.size(); int n=obstacleGrid[0].size(); int **ary1; ary1=new int *[m]; for(int i=0;i<m;i++) ary1[i]=new int ; for(int i=0;i<m;i++) for(int j=0;j<n;j++) ary1[i][j]=obstacleGrid[i][j]-1; for(int i=m-1;i>=0;i--) for(int j=n-1;j>=0;j--) if(ary1[i][j]!=0) { if(i==m-1&&j==n-1) ary1[i][j]=1; else if(i==m-1&&j<n-1) ary1[i][j]=ary1[i][j+1]; else if(i<m-1&&j==n-1) ary1[i][j]=ary1[i+1][j]; else ary1[i][j]=ary1[i+1][j]+ary1[i][j+1]; } int last_result=ary1[0][0]; for(int i=0;i<m;i++) delete[]ary1[i]; delete[]ary1; return last_result; } int main() { }
相关文章推荐
- jeasyui
- request.getContextPath(),request.getServletPath() 区别
- sequelize的使用
- 探究String、StringBuffer、StringBuilder的区别?
- 【趋势科技实习录】UI添加滚动条
- leetcode_62题——Unique Paths (动态规划)
- OC视频笔记4.4(NSNumber的使用方法)4.5(NSValue的使用)(NSNull的使用)
- [iOS]UITableViewController完成收回键盘操作
- STL容器之deque
- #pragma mark - TableView Optional Methods - (void)tableView:(UITableView *)tableView didSelectRowAt
- 优化UITableViewCell高度计算的那些事
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- Java中String,StringBuilder和StringBuffer的区别
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- 文字向上滚动实现代码只jQuey
- Libevent0.1学习之queue.h
- iOS开发 动画 UIDynamicAnimator
- QT UI美化 布局
- iOS学习(2)——UILabel
- arduino 超声波小车