"063-unique Paths II (unique path Problem II)""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original Question
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 .
Note:m and N would be is at most 100 .
Main Topic
The only path problem follows, if there is a barrier in the path, the total number of paths to be obtained. "Unique path Problem"
Note: the number of rows and columns in the grid does not exceed 100
Thinking of solving problems
Using divide-and-conquer solution method
Save the result with a m*n of group A.
For elements in a array, there are.
1, when x=0 or y=0, and (x, y) position barrier-free. There is a[x][y] = 1; there are 0 obstacles.
2, when X>=1 and Y>=1, and (x, y) position barrier-free. with a[x][y] = a[x-1][y]+a[x][y-1]. There's a handicap, 0.
3. The knot to be asked is a[m-1][n-1].
Code Implementation
Algorithm implementation class
Public classSolution { Public int Uniquepathswithobstacles(int[] obstaclegrid) {//Input checksum if(Obstaclegrid = =NULL|| Obstaclegrid.length <1|| obstaclegrid[0].length <1|| obstaclegrid[0][0] ==1|| Obstaclegrid[obstaclegrid.length-1][obstaclegrid[0].length-1] ==1) {return 0; }introws = obstaclegrid.length;intcols = obstaclegrid[0].length;int[[] result =New int[Rows] [Cols];//The first position has how many ways, barrier-free is 1, there are 0 kinds of obstaclesresult[0][0] = obstaclegrid[0][0] ==0?1:0; for(inti =1; i < cols; i++) {result[0][i] = obstaclegrid[0][i] = =0? result[0][i-1] :0; } for(inti =1; i < rows; i++) {result[i][0] = obstaclegrid[i][0] ==0? Result[i-1][0] :0; } for(inti =1; i < rows; i++) { for(intj =1; J < cols; J + +) {Result[i][j] = obstaclegrid[i][j] = =0? Result[i-1][J] + result[i][j-1] :0; } }returnResult[rows-1][cols-1]; }//Use recursive method to time out Public int UniquePathsWithObstacles2(int[] obstaclegrid) {//Input checksum if(Obstaclegrid = =NULL|| Obstaclegrid.length <1|| obstaclegrid[0].length <1|| Obstaclegrid[obstaclegrid.length-1][obstaclegrid[0].length-1] ==1) {return 0; }int[] result = {0}; Solve (Obstaclegrid,0,0, result);returnresult[0]; } Public void Solve(int[] Grid,intRowintColint[] sum) {//reach the end point if(Row = = Grid.length-1&& col = = grid[0].length-1) {sum[0]++; }//Not to the end point, dot within the chessboard, and the current position is not Else if(Row >=0&& Row < grid.length && Col >=0&& Col < grid[0].length && Grid[row][col] = =0) {//GO rightSolve (grid, row, col +1, sum);//Go downSolve (grid, row +1, col, sum); } }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47182723"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
leetcode-Interview Algorithm classic-java Implementation "" 063-unique Paths II (unique path Problem II) "