[LeetCode]Unique Paths I & II
2013-03-17 08:41
441 查看
I. A robot is located at the top-left corner of a m x n grid
(marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
1. 递归。但无法通过大集合
2. dynamic programming:
# path to [i][j] = #path to [i-1][j] + #path to [i][j-1]
II:
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
in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
The total number of unique paths is
与上题类似,设置unreachable number of path 为-1表示不可达。
一个更简洁的答案:http://fisherlei.blogspot.com/2013/01/leetcode-unique-paths-ii.html
用一个一位数组表示t[i][j]即可。
(marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
1. 递归。但无法通过大集合
class Solution { public: int uniquePaths(int m, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int paths=0; int i=1; int j=1; helper(m,n,i,j,paths); return paths; } void helper(int m, int n, int i, int j, int &paths) { if(i==m && j==n) { paths++; return; } if(i<m) { helper(m,n,i+1,j,paths); } if(j<n) { helper(m,n,i,j+1,paths); } } };
2. dynamic programming:
# path to [i][j] = #path to [i-1][j] + #path to [i][j-1]
class Solution { public: int uniquePaths(int m, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int>> t; t.resize(m,vector<int>(n)); for(int i=0;i<m;i++) { t[i][0]=1; } for(int i=0;i<n;i++) { t[0][i]=1; } for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { t[i][j]=t[i-1][j]+t[i][j-1]; } } return t[m-1][n-1]; } };
II:
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.
与上题类似,设置unreachable number of path 为-1表示不可达。
class Solution { public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { // Start typing your C/C++ solution below // DO NOT write int main() function if(obstacleGrid.size() == 0) return 0; for(int i=0;i<obstacleGrid.size();i++) { for(int j=0;j<obstacleGrid.at(0).size();j++) { if(obstacleGrid[i][j]==1) obstacleGrid[i][j]=-1; } } if(obstacleGrid[0][0]!=-1) obstacleGrid[0][0]=1; for(int i=1; i<obstacleGrid.size();i++) { if(obstacleGrid[i][0]!=-1 && obstacleGrid[i-1][0]==1) obstacleGrid[i][0]=1; else obstacleGrid[i][0]=-1; } for(int i=1; i<obstacleGrid.at(0).size();i++) { if(obstacleGrid[0][i]!=-1 && obstacleGrid[0][i-1]!=-1) obstacleGrid[0][i]=1; else obstacleGrid[0][i]=-1; } for(int i=1;i<obstacleGrid.size();i++) { for(int j=1;j<obstacleGrid.at(0).size();j++) { if(obstacleGrid[i][j]==-1) continue; if(obstacleGrid[i-1][j]==-1) { if(obstacleGrid[i][j-1]!=-1) obstacleGrid[i][j]=obstacleGrid[i][j-1]; else obstacleGrid[i][j]=-1; } else{ if(obstacleGrid[i][j-1]!=-1) obstacleGrid[i][j]=obstacleGrid[i-1][j]+obstacleGrid[i][j-1]; else obstacleGrid[i][j]=obstacleGrid[i-1][j]; } } } if( obstacleGrid[obstacleGrid.size()-1][obstacleGrid.at(0).size()-1] == -1) return 0; else return obstacleGrid[obstacleGrid.size()-1][obstacleGrid.at(0).size()-1]; } };
一个更简洁的答案:http://fisherlei.blogspot.com/2013/01/leetcode-unique-paths-ii.html
用一个一位数组表示t[i][j]即可。
相关文章推荐
- leetcode-62&63 Unique Paths I & II
- Leetcode-Unique Paths&Unique PathsII
- LeetCode 62 Unique Paths & 63 Unique Paths II
- LeetCode: Unique Paths I & II & Minimum Path Sum
- leetcode - 62,63. Unique Paths(II) & 64.Minimum Path Sum
- 【LeetCode】Unique Paths I && II && Minimum Path Sum
- leetcode--Unique Paths && Unique Paths ii
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
- leetcode--Unique Paths && Unique Paths ii
- 【leetcode】Unique Paths I & II
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
- LeetCode | Unique Paths & II & Minimum Path Sum
- Leetcode | Unique Paths I & II
- leetcode -- Unique Paths I &&II-- 典型DP 题目,简单要看
- 【LeetCode】Unique Paths I && II
- [Leetcode] #62#63 Unique Paths I & II
- leetcode Unique Paths & Unique Paths II & Minimum Path Sum
- 【leetcode】Unique Paths II(动态规划)
- 【leetcode】Array——Unique Paths II(63)
- LeetCode-Unique Paths II