LeetCode //C - 63. Unique Paths II

63. Unique Paths II

You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle.

Return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The testcases are generated so that the answer will be less than or equal to 2 ∗ 1 0 9 . 2 * 10^9. 2109.
 

Example 1:

LeetCode //C - 63. Unique Paths II_第1张图片

Input: obstacleGrid = [[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
Example 2:

LeetCode //C - 63. Unique Paths II_第2张图片

Input: obstacleGrid = [[0,1],[0,0]]
Output: 1

Constraints:
  • m == obstacleGrid.length
  • n == obstacleGrid[i].length
  • 1 <= m, n <= 100
  • obstacleGrid[i][j] is 0 or 1.

From: LeetCode
Link: 63. Unique Paths II


Solution:

Ideas:

This function assumes that the memory for obstacleGrid has already been allocated and that obstacleGridSize and obstacleGridColSize are correctly set to reflect the dimensions of obstacleGrid. The dp matrix uses long long to avoid integer overflow for large grids. The function initializes the first row and column based on the presence of obstacles and then iterates over the rest of the grid, setting the value of dp[i][j] to 0 if there’s an obstacle at obstacleGrid[i][j], or to the sum of dp[i-1][j] and dp[i][j-1] otherwise. After calculating the number of unique paths, the function cleans up the allocated memory for dp and returns the result.

Code:
int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obstacleGridColSize) {
    // If the start or end is an obstacle, return 0 immediately.
    if(obstacleGrid[0][0] == 1 || obstacleGrid[obstacleGridSize - 1][obstacleGridColSize[0] - 1] == 1) {
        return 0;
    }
    
    // Allocate space for the dp matrix
    long long **dp = (long long **)malloc(obstacleGridSize * sizeof(long long *));
    for(int i = 0; i < obstacleGridSize; i++) {
        dp[i] = (long long *)malloc(obstacleGridColSize[0] * sizeof(long long));
    }
    
    // Initialize the top-left corner
    dp[0][0] = 1;
    
    // Fill the first column
    for(int i = 1; i < obstacleGridSize; i++) {
        dp[i][0] = (obstacleGrid[i][0] == 1 || dp[i-1][0] == 0) ? 0 : 1;
    }
    
    // Fill the first row
    for(int j = 1; j < obstacleGridColSize[0]; j++) {
        dp[0][j] = (obstacleGrid[0][j] == 1 || dp[0][j-1] == 0) ? 0 : 1;
    }
    
    // Build the rest of the dp table
    for(int i = 1; i < obstacleGridSize; i++) {
        for(int j = 1; j < obstacleGridColSize[0]; j++) {
            if(obstacleGrid[i][j] == 1) {
                dp[i][j] = 0;
            } else {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
    }
    
    // The bottom-right corner has the number of unique paths
    int result = dp[obstacleGridSize - 1][obstacleGridColSize[0] - 1];
    
    // Free the dp matrix
    for(int i = 0; i < obstacleGridSize; i++) {
        free(dp[i]);
    }
    free(dp);
    
    return result;
}

你可能感兴趣的:(LeetCode,C/C++,leetcode,c语言,算法)