"Leetcode-Interview algorithm classic-java Implementation" "063-unique Paths II (unique path Problem II)"

Source: Internet
Author: User

"063-unique Paths II (unique path Problem II)""leetcode-Interview algorithm classic-java Implementation" "All Topics folder 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 is maybe. Suppose there are obstacles in the path, and the total number of paths is calculated.

"Unique path Problem"
  Note: the number of rows and columns in the grid does not exceed 100

Thinking of solving problems

The method of solving the problem by divide and conquer
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.

with a[x][y] = 1. There's a handicap, 0.
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. No obstacle 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]++; }//Do not reach the end point in the board. And the current location 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); } }}

Assessment Results

  Click on the picture, the mouse does not release, drag a position, release after the new form to view the full picture.

Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47182723"

"Leetcode-Interview algorithm classic-java Implementation" "063-unique Paths II (unique path Problem II)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.