您的位置:首页 > 产品设计 > UI/UE

[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. 递归。但无法通过大集合

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
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
.

与上题类似,设置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]即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: