LeetCode学习刷题-简单动态规划63

3,784 阅读2分钟

「这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战

题目

63. 不同路径 II

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?网格中的障碍物和空位置分别用 1 和 0 来表示。

image.png

示例 1:
输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
输出:2
解释:
3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有 2 条不同的路径:

  1. 向右 -> 向右 -> 向下 -> 向下
  2. 向下 -> 向下 -> 向右 -> 向右

思路

比较简单的动态规划题目,每一个格子的方案由上一个格子与左边格子的结果相加得到,上一个格子或者左边格子可能是空的或者障碍物,如果左边有障碍物,此时该格子由上面格子的结果继承得到,同理上面格子如果有障碍物则由左边继承得到,继承这个词不是特别合适,暂说这样的吧。 动态规划方程如下

image.png

有一些需要注意的地方,有些是我开始没想到的地方,确实是失误,下次同类题目需要注意:
1.开始或者结束存在障碍物
2.一条或者一列的情况,这时候没有障碍物是1,有障碍物是0
3.若格子是tempArray[i][j],则tempArray[i][j] = tempArray[i - 1][j] + tempArray[i][j - 1];
4.一定要注意边界条件以及同时符合的限制条件

代码

    <script>
        var uniquePathsWithObstacles = function (obstacleGrid) {
            let tempArray = obstacleGrid;
            let rowlength = tempArray.length; //行
            let collength = tempArray[0].length; //列
            let res;
            // 对单行或者单列情况进行判断,只要中间有一个有障碍物结果为0,否则为1
            if (rowlength === 1 || collength === 1) {
                let bool = false;
                for (let i = 0; i < rowlength; i++) {
                    for (let j = 0; j < collength; j++) {
                        if (tempArray[i][j] === 1) {
                            bool = true;
                            break;
                        }
                    }
                }
                if (bool) {
                    console.log(0);
                    return;
                } else {
                    console.log(1);
                    return;
                }
            }
            // 对起始和结尾进行判断,有一个有障碍物则结果直接返回0
            if (tempArray[rowlength - 1][collength - 1] === 1 || tempArray[0][0] === 1) {
                console.log(0);
                return;
            }

            // 怕对结果会产生影响,数组中的障碍物1全部变为-1
            // i为行数,j为列数
            for (let i = 0; i < rowlength; i++) {
                for (let j = 0; j < collength; j++) {
                    if (tempArray[i][j] === 1) {
                        tempArray[i][j] = -1;
                    }
                }
            }
            // 对第一行和第一列的数据进行初始化,有一个障碍物后面全部为-1
            let rowTemp = 1;
            for (let j = 0; j < collength; j++) {
                if (tempArray[0][j] === -1) {
                    rowTemp = -1;
                    tempArray[0][j] = rowTemp;
                } else {
                    tempArray[0][j] = rowTemp;
                }
            }
            let colTemp = 1;
            for (let i = 0; i < rowlength; i++) {
                if (tempArray[i][0] === -1) {
                    colTemp = -1;
                    tempArray[i][0] = colTemp;
                } else {
                    tempArray[i][0] = colTemp;
                }
            }

            for (let i = 1; i < rowlength; i++) {
                for (let j = 1; j < collength; j++) {
                    if (tempArray[i][j] !== -1) {
                        tempArray[i][j] = tempArray[i - 1][j] + tempArray[i][j - 1];
                    }
                    if (tempArray[i - 1][j] === -1 && tempArray[i][j] !== -1) {
                        tempArray[i][j] = tempArray[i][j - 1]
                    }
                    if (tempArray[i][j - 1] === -1 && tempArray[i][j] !== -1) {
                        tempArray[i][j] = tempArray[i - 1][j]
                    }
                }
            }
            if (tempArray[rowlength - 1][collength - 1] === -1) {
                res = 0
            } else {
                res = tempArray[rowlength - 1][collength - 1];
            }

            console.log(res);
            console.log(tempArray);
        };

        uniquePathsWithObstacles([
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0
            ],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0
            ]
        ])
        uniquePathsWithObstacles([
            [0, 0, 0],
            [0, 1, 0],
            [0, 0, 0]
        ])
        uniquePathsWithObstacles([
            [0, 1],
            [0, 0]
        ])
        uniquePathsWithObstacles([
            [0, 0],
            [0, 1]
        ])
        uniquePathsWithObstacles([
            [0, 0, 1, 0]
        ])
        uniquePathsWithObstacles([
            [0, 1],
            [1, 0]
        ])
        uniquePathsWithObstacles([
            [0, 0],
            [1, 1],
            [0, 0]
        ])
        uniquePathsWithObstacles([
            [0, 1, 0],
            [1, 0, 0],
            [0, 0, 0]
        ])
        uniquePathsWithObstacles([
            [0, 1, 0, 0],
            [0, 0, 0, 0],
            [1, 0, 1, 0],
            [0, 0, 1, 0]
        ])
        uniquePathsWithObstacles([
            [0, 1, 0, 0, 0],
            [1, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0]
        ])
    </script>

说明

本文是作者用于学习时,在LeetCode刷题所见所想,引用了部分力扣资源,代码是根据大佬们思路进行写的,比起大佬们的代码,我的代码肯定有很大的优化空间了,如果是学习的建议访问力扣官网进行学习,本博客仅记录我的成长过程!