Description
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).
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.
Note: m and n will be at most 100.
Example
Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
- Right -> Right -> Down -> Down
- Down -> Down -> Right -> Right
解题思路
没啥思路,和62一样的,堵住了在对应位置重置为0就行
代码
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int answer[][] = new int[m][n];
if(obstacleGrid[0][0] == 1)
return 0;
answer[0][0] = 1;
int i, j;
for(i = 1; i < m; i++){
if(obstacleGrid[i][0] == 1){
answer[i][0] = 0;
}
else
answer[i][0] = answer[i - 1][0];
}
for(i = 1; i < n; i++){
if(obstacleGrid[0][i] == 1){
answer[0][i] = 0;
}
else
answer[0][i] = answer[0][i - 1];
}
for(i = 1; i < m; i++){
for(j = 1; j < n; j++){
if(obstacleGrid[i][j] == 1)
answer[i][j] = 0;
else{
answer[i][j] = answer[i - 1][j] + answer[i][j - 1];
}
}
}
return answer[m - 1][n - 1];
}
}