代码随想录Leetcode63. 不同路径 II

题目:

代码随想录Leetcode63. 不同路径 II_第1张图片


代码(首刷自解 2024年2月20日):

初始化时注意若有阻碍,则后面都为0

class Solution {
public:
    int uniquePathsWithObstacles(vector>& obstacleGrid) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        /*dp数组表示路径条数*/
        vector> dp(m, vector(n, 0));
        /*初始化*/
        for (int i = 0; i < m && obstacleGrid[i][0] != 1; ++i) dp[i][0] = 1;
        for (int i = 0; i < n && obstacleGrid[0][i] != 1; ++i) dp[0][i] = 1;

        for (int i = 1; i < m; ++i) {
            for (int j = 1; j < n; ++j) {
                if (obstacleGrid[i][j] == 1) {
                    dp[i][j] = 0;
                    cout<<"!dp["<

你可能感兴趣的:(#,leetcode,---medium,算法,leetcode,动态规划)