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.
Test instructions: There is a number of robot paths with obstacles.
Train of thought: more than a judge of the situation of obstacles, the other unchanged.
Class Solution {public: int uniquepathswithobstacles (vector<vector<int> > &obstaclegrid) {vector <vector<int> > F (obstaclegrid.size (), vector<int> (Obstaclegrid[0].size ())) f[0][0] = Obstaclegrid [0] [0] = = 1? 0:1;for (int i = 1; i < f.size (); i++) f[i][0] = obstaclegrid[i][0] = = 1? 0:f[i-1][0];for (int i = 1; i < f[0].size (); i++) f[0][i] = obstaclegrid[0][i] = = 1? 0:f[0][i-1];for (int i = 1; i < f.size (); i++) for (int j = 1; J < F[i].size (), j + +) F[i][j] = obstaclegrid[i][j] = = 1? 0: (F[i-1][j] + f[i][j-1]); return f[f.size () -1][f[0].size ()-1]; }};
Leetcode Unique Paths II