其他分享
首页 > 其他分享> > 63.不同路径Ⅱ

63.不同路径Ⅱ

作者:互联网

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int row = obstacleGrid.size();
        int column = obstacleGrid[0].size();
        vector<vector<int>> dp(row, vector<int>(column, 0));
        for (int i = 0; i < row && obstacleGrid[i][0] == 0; ++i) {//初始化第一列:与62题的区别在于一旦某处出现了障碍,后面都无法到达,只能初始化为0
            dp[i][0] = 1;
        }
        for (int j = 0; j < column && obstacleGrid[0][j] == 0; ++j) {//初始化第一行:同上
            dp[0][j] = 1;
        }
        for (int i = 1; i < row; ++i) {
            for (int j = 1; j < column; ++j) {
                if (obstacleGrid[i][j] == 1) dp[i][j] = 0;//与62的区别在于:某处有障碍,代表无法到达
                else dp[i][j] = dp[i][j - 1] + dp[i - 1][j];//没障碍,才能用dp公式更新;
            }
        }  
        return dp[row - 1][column - 1];
    }
};

标签:obstacleGrid,int,不同,路径,column,62,63,dp,row
来源: https://blog.csdn.net/jiuri1005/article/details/113814495