## Unique Paths II

### 描述

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

### 备忘录法

// Unique Paths II
// 深搜 + 缓存，即备忘录法
class Solution {
public:
int uniquePathsWithObstacles(const vector<vector<int> >& obstacleGrid) {
const int m = obstacleGrid.size();
const int n = obstacleGrid[0].size();
if (obstacleGrid[0][0] || obstacleGrid[m - 1][n - 1]) return 0;

f = vector<vector<int> >(m, vector<int>(n, 0));
f[0][0] = obstacleGrid[0][0] ? 0 : 1;
return dfs(obstacleGrid, m - 1, n - 1);
}
private:
vector<vector<int> > f;  // 缓存

// @return 从 (0, 0) 到 (x, y) 的路径总数
int dfs(const vector<vector<int> >& obstacleGrid,
int x, int y) {
if (x < 0 || y < 0) return 0; // 数据非法，终止条件

// (x,y)是障碍
if (obstacleGrid[x][y]) return 0;

if (x == 0 and y == 0) return f[0][0]; // 回到起点，收敛条件

if (f[x][y] > 0) {
return f[x][y];
} else {
return f[x][y] = dfs(obstacleGrid, x - 1, y) +
dfs(obstacleGrid, x, y - 1);
}
}
};


### 动规

// Unique Paths II
// 动规，滚动数组
// 时间复杂度O(n^2)，空间复杂度O(n)
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
const int m = obstacleGrid.size();
const int n = obstacleGrid[0].size();
if (obstacleGrid[0][0] || obstacleGrid[m-1][n-1]) return 0;

vector<int> f(n, 0);
f[0] = obstacleGrid[0][0] ? 0 : 1;

for (int i = 0; i < m; i++) {
f[0] = f[0] == 0 ? 0 : (obstacleGrid[i][0] ? 0 : 1);
for (int j = 1; j < n; j++)
f[j] = obstacleGrid[i][j] ? 0 : (f[j] + f[j - 1]);
}

return f[n - 1];
}
};