1. 程式人生 > >LeetCode 63 _ Unique Paths II 全部不同路徑2

LeetCode 63 _ Unique Paths II 全部不同路徑2

分享 一個數 == sage 路徑 example tps 要求 col

Description:

A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

技術分享圖片

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

Input:
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

Solution:

這道題的要求是求從m*n的方格左上行至右下的所有路徑總數,路中若有障礙則不能通行

這道題主要用到了動態規劃的思想,所謂動態規劃呢,就是將一個大問題分成多個小問題,找到每個小問題的最優解,最終得到最後的結果。

落實到這一題上,就是依次對每一個方格進行遍歷。

首先先看這個方格是否有障礙物,如果有,那麽這個方格被遍歷到的可能性為0;

如果沒有障礙物,當它是首行或首列時,它的值就等於它前一個位置的值。這裏我們發現必須先設定一個起點的初值,否則就全部都是0啦!若起點無障礙物,則將起點值設置為1,表示起點會被遍歷一次;

如果無障礙物且不是首行或首列時,它被遍歷到的可能性就是它左邊和上邊的被遍歷到的可能性之和。

最後輸出最後一格的值即為答案。

Code:

public int uniquePathsWithObstacles(int[][] obstacleGrid) {
   int row = obstacleGrid.length;
    int col = obstacleGrid[0].length;

    if (obstacleGrid[0][0] == 1){
        return 0;
    }

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            if (obstacleGrid[i][j] == 1){  // 如果此處有障礙,那麽無路可通
                obstacleGrid[i][j] = 0;
            }else if (i == 0 && j == 0){  // 對第一個數賦值
                obstacleGrid[i][j] = 1;
            }else if (i == 0){
                obstacleGrid[i][j] = obstacleGrid[i][j-1];
            }else if (j == 0){
                obstacleGrid[i][j] = obstacleGrid[i-1][j];
            }else{
                obstacleGrid[i][j] = obstacleGrid[i][j-1] + obstacleGrid[i-1][j];
            }
        }
    }

    return obstacleGrid[row - 1][col - 1];
}

  

提交情況:

Runtime: 0 ms, faster than 100.00% of Java online submissions for Unique Paths II. Memory Usage: 38.8 MB, less than 22.61% of Java online submissions for Unique Paths II.

LeetCode的標答是這樣寫的,我覺得沒必要單獨將第一行和第一列拎出來遍歷呀,不過這樣看起來更清楚一點,最後運行的效率和上面那種差不多,代碼如下:

public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    int row = obstacleGrid.length;
    int col = obstacleGrid[0].length;

    if (obstacleGrid[0][0] == 1){
        return 0;
    }

    obstacleGrid[0][0] = 1;

    for (int i = 1; i < row; i++){
        if (obstacleGrid[i][0] == 0 && obstacleGrid[i-1][0] == 1){
            obstacleGrid[i][0] = 1;
        }else{
            obstacleGrid[i][0] = 0;
        }
    }

    for (int i = 1; i < col; i++){
        if (obstacleGrid[0][i] == 0 && obstacleGrid[0][i-1] == 1){
            obstacleGrid[0][i] = 1;
        }else{
            obstacleGrid[0][i] = 0;
        }
    }        

    for (int i = 1; i < col; i++){
        for (int j = 1; j < row; j++){
            if (obstacleGrid[j][i] == 1){
                obstacleGrid[j][i] = 0;
            }else{
                obstacleGrid[j][i] = obstacleGrid[j-1][i] +obstacleGrid[j][i-1];
            }
        }
    }

    return obstacleGrid[row-1][col-1];
}

運行情況:

Runtime: 0 ms, faster than 100.00% of Java online submissions for Unique Paths II. Memory Usage: 38.3 MB, less than 29.66% of Java online submissions for Unique Paths II.

LeetCode 63 _ Unique Paths II 全部不同路徑2