One Day together Leetcode (i) Title
Follow up for "Unique Paths":
Now consider if some obstacles is added to the grids. How many unique paths would there be?
An obstacle and empty space are 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.
(ii) Problem solving
Problem-solving ideas: Refer to the previous blog post "One Day together Leetcode" #62. Unique Paths
classSolution { Public:intdp[101][101];intUniquepathswithobstacles ( vector<vector<int>>& Obstaclegrid) {introw = Obstaclegrid.size ();intCol =0;if(row!=0) col = obstaclegrid[0].size ();if(obstaclegrid[0][0]==1)return 0;//Starting point not to be returned directly 0 for(inti = row-1; i>=0; i--) for(intj = col-1; j>=0; j--) {if(obstaclegrid[i][j]==1) Dp[i][j] =0;//Representative blocked Else if(i==row-1&&j==col-1) Dp[i][j] =1;//The DP at the specified end is 1 ElseDP[I][J] = dp[i+1][j]+dp[i][j+1]; }returndp[0][0]; }
"One Day together Leetcode" #63. Unique Paths II