Follow up for "unique paths ":
Now consider if some obstacles are added to the grids. How many unique paths wocould there be?
An obstacle and empty space is marked1And0Respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as partitioned strated below.
[ [0,0,0], [0,1,0], [0,0,0]]
The total number of unique paths is2.
Note: MAndNWill be at most 100.
Class solution {public: int uniquepathswithobstacles (vector <int> & obstaclegrid) {int rows = obstaclegrid. size (); If (rows <1) return rows; int Cols = obstaclegrid [0]. size (); If (Cols <1) return Cols; vector <int> temp (cols, 1); vector <int> res (rows, temp ); // number of paths for RES records to reach the corresponding position for (INT I = 0; I <rows; I ++) {for (Int J = 0; j <Cols; j ++) {If (obstaclegrid [I] [J] = 1) RES [I] [J] = 0; else if (I = 0 & J! = 0) RES [I] [J] = res [I] [J-1]; else if (j = 0 & I! = 0) RES [I] [J] = res [I-1] [J]; else if (I! = 0 & J! = 0) RES [I] [J] = res [I-1] [J] + Res [I] [J-1];} // end for return res [rows-1] [Cols-1] ;}};