力扣第63题 不同路径 II

力扣第63题 不同路径 II

class Solution {
    public:
    int func(int m, int n, vector<vector<int>>& obstacleGrid, vector<vector<unsigned long long>>& dp)
    {
        if (m < 0 || n < 0)
        {
            return 0;
        }
        

        if (!dp[m][n])
        {
            if (!m || !n)
            {
                if (!m && !n)
                    dp[m][n] = !obstacleGrid[m][n] ? 1 : 0;
                else
                    dp[m][n] = !obstacleGrid[m][n] && (func(m - 1, n, obstacleGrid, dp) || func(m, n - 1, obstacleGrid, dp)) ? 1 : 0;
            }
            else
                dp[m][n] = obstacleGrid[m][n] ? 0 : func(m - 1, n, obstacleGrid, dp) + func(m, n - 1, obstacleGrid, dp);
        }
        return dp[m][n];
    }
    
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid)
    {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        vector<vector<unsigned long long>> dp(m, vector<unsigned long long>(n, 0));
        return func(m-1, n-1, obstacleGrid, dp);
    }

};
原文地址:https://www.cnblogs.com/woodjay/p/12378501.html