[LeetCode-interview algorithm classic-Java implementation] [063-Unique Paths II (Unique path Question II)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [063-Unique Paths II (Unique path Question II)], leetcode -- java
[063-Unique Paths II (Unique path Problem II )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question

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 marked as 1 and 0 respectively 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: m and n will be at most100.

Theme

If there is an obstacle in the path, find the total number of paths. [Unique path problem]
  Note:The number of rows and columns in the grid cannot exceed 100

Solutions

Divide and conquer
Save the result with m * n group.
For elements in array.
1. When x = 0 or y = 0, and (x, y) is not accessible. A [x] [y] = 1; 0
2. When x> = 1 and y> = 1, and (x, y) is not accessible. There is A [x] [y] = A [x-1] [y] + A [x] [Y-1]. The obstacle is 0.
3. The requested node is A [s-1] [n-1].

Code Implementation

Algorithm Implementation class

Public class Solution {public int uniquePathsWithObstacles (int [] [] obstacleGrid) {// input validation 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;} int rows = obstacleGrid. length; int cols = obstacleGrid [0]. length; int [] [] result = new int [rows] [cols]; // The first How many methods are there for location? One is accessibility, and the other is 0 results [0] [0] = obstacleGrid [0] [0] = 0? 1: 0; for (int I = 1; I <cols; I ++) {result [0] [I] = obstacleGrid [0] [I] = 0? Result [0] [I-1]: 0;} for (int I = 1; I <rows; I ++) {result [I] [0] = obstacleGrid [I] [0] = 0? Result [I-1] [0]: 0 ;}for (int I = 1; I <rows; I ++) {for (int j = 1; j <cols; j ++) {result [I] [j] = obstacleGrid [I] [j] = 0? Result [I-1] [j] + result [I] [j-1]: 0;} return result [rows-1] [cols-1];} // recursive method will time out public int uniquePathsWithObstacles2 (int [] [] obstacleGrid) {// input validation 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); return result [0];} public void solve (int [] [] grid, int row, int col, int [] sum) {// if (row = grid. length-1 & col = grid [0]. length-1) {sum [0] ++;} // No end point exists. The current position is inside the chessboard and is not else if (row> = 0 & row <grid. length & col> = 0 & col <grid [0]. length & grid [row] [col] = 0) {// go to the Right to solve (grid, row, col + 1, sum); // go down to solve (grid, row + 1, col, sum );}}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/4252723]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.