Unique Paths II
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 and respectively in the 1 0 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 .
Note: m and N would be is at most 100.
With the above question difference is not big, only need to judge has the obstacle to place 0 can.
For the first row column, the first obstacle and the number of paths followed are 0
classSolution { Public: intUniquepathswithobstacles (vector<vector<int> > &Obstaclegrid) { if(Obstaclegrid.empty ())return 0; Else if(obstaclegrid[0].empty ())return 0; Else {//m>=1, N>=1 intm =obstaclegrid.size (); intn = obstaclegrid[0].size (); Vector<vector<int> > Count (M, vector<int> (n,0)); for(inti =0; I < m; i + +) { if(obstaclegrid[i][0] ==0) count[i][0] =1; Else Break; } for(intj =0; J < N; J + +) { if(obstaclegrid[0][J] = =0) count[0][J] =1; Else Break; } for(inti =1; I < m; i + +) { for(intj =1; J < N; J + +) { if(Obstaclegrid[i][j] = =1) Count[i][j]=0; ElseCount[i][j]= count[i-1][j]+count[i][j-1]; } } returncount[m-1][n-1]; } }};
"Leetcode" Unique Paths II