https://leetcode.com/problems/unique-paths-ii/description/
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.
解题思路:
dp[i][j] 表示走到i,j位置的总的路径数量
状态转移方程:
dp[i][j] = dp[i-1][j]+dp[i][j-1] if(obstacleGrid[i][j]!=1)
dp[i][j] = 0 if(obstacleGrid[i][j]==1)
边界
dp[i][0] = 1 if(obstacleGrid[k][0]==0) 0<k<i
dp[i][0] = 0 if(obstacleGrid[k][0]==1) 0<k<i
public class Unique_Paths_II_63 {
/**
* dp[i][j] 表示走到i,j位置的总的路径数量
*
* dp[i][j] = dp[i-1][j]+dp[i][j-1] if(obstacleGrid[i][j]!=1)
* if(obstacleGrid[i][j]==1) dp[i][j] = 0
*
* 边界
* dp[i][0] = 1 if(obstacleGrid[k][0]==0) 0<k<i
* dp[i][0] = 0 if(obstacleGrid[k][0]==1) 0<k<i
*
* @param obstacleGrid
* @return
*/
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null || obstacleGrid.length == 0)
return 0;
int n = obstacleGrid.length;
int m = obstacleGrid[0].length;
int[][] dp = new int[n][m];
boolean noObstacle = true;
for(int i=0;i<n;i++){
if(obstacleGrid[i][0]==1)noObstacle = false;
if(obstacleGrid[i][0]==0&&noObstacle){
dp[i][0] = 1;
}else{
dp[i][0] = 0;
}
}
noObstacle = true;
for(int j=0;j<m;j++){
if(obstacleGrid[0][j]==1)noObstacle = false;
if(obstacleGrid[0][j]==0&&noObstacle){
dp[0][j] = 1;
}else{
dp[0][j] = 0;
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (obstacleGrid[i][j] == 1) {
dp[i][j] = 0;
}
if (obstacleGrid[i][j] != 1) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[n - 1][m - 1];
}
public static void main(String[] args) {
new Unique_Paths_II_63().uniquePathsWithObstacles(
new int[][]{{0,0,0},
{0,1,0},
{0,0,0}});
}
}