题目:
解答:
如果有障碍,则令该格不可通过。
http://www.cnblogs.com/qianye/archive/2013/09/06/3305680.html
代码:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int a[101][101];
memset(a, 0, sizeof(int)*101 * 101);
int m = obstacleGrid.size();
int n = obstacleGrid.begin()->size();
if (obstacleGrid[0][0] == 1 || m <= 0 || n <= 0) //如果第一个格为障碍,则不需要计算,其他的肯定不可达
return 0;
a[0][0] = 1;
for (int i = 1; i < m; i++)
{
if (obstacleGrid[i][0] == 1)
a[i][0] = 0;
else
a[i][0] = a[i-1][0]; //这里要注意 不能简单的赋值为1, 要根据上一步的结果赋值
}
for (int i = 1; i < n; i++)
{
if (obstacleGrid[0][i] == 1)
a[0][i] = 0;
else
a[0][i] = a[0][i-1]; //这里要注意 不能简单的赋值为1, 要根据上一步的结果赋值
}
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
if (obstacleGrid[i][j] == 1)
a[i][j] = 0;
else
a[i][j] = a[i][j - 1] + a[i - 1][j];
}
}
return a[m - 1][n - 1];
}
};