LeetCode_Array_63. Unique Paths II (C++)

目录

1,题目描述

2,思路

3,代码

4,测试结果


1,题目描述

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

 

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

Input:
[
  [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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/unique-paths-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

2,思路

整体思路与LeetCode_Array_62. Unique Paths (C++)差不多,只不过有以下几点需要注意:

  1. 这次的输入为数组,故可以在输入矩阵的基础上进行赋值,从而使空间复杂度为O(1),然而Java语言可以通过,C++会产生溢出错误,因此,使用C++解决此题需重新声明类型为unsigned int 或 long类型的数组,方可进行下一步;
  2. 第一行与第一列的初始化:以第一行为例,若当前行中含有元素为1,即出现障碍,则其后面对应的初值均应设置为0,否则设置为1.即:obstacleGrid[0][i] == 1 ? dp[i] = 0 : dp[i] = dp[i-1];
  3. 遍历矩阵内部时,若对应元素为1,则将其对应的数组的值置0(表示此方格对附近的方格步数 的增加无增益效果);

3,代码

空间复杂度O(n)

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
       
        int row = obstacleGrid.size();
        int col = obstacleGrid[0].size();
        
        unsigned int dp[col];//使用int会溢出
        if(obstacleGrid[0][0] == 1 || obstacleGrid[row-1][col-1] == 1) return 0;//起/终点放置障碍物

        dp[0] = 1;

        //初始化第一行
        for(int i = 1 ; i < col ; i++) obstacleGrid[0][i] == 1 ? dp[i] = 0 : dp[i] = dp[i-1];
        

        for(int i = 1 ; i < row ; i++){
            if(obstacleGrid[i][0] == 1) dp[0] = 0 ;
            for(int j = 1 ; j < col ; j++){
                obstacleGrid[i][j] == 1 ? dp[j] = 0 : dp[j] += dp[j-1];
            }
        }
        return dp[col-1];
    }
};

空间复杂度O(m*n)

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
       
        int row = obstacleGrid.size();
        int col = obstacleGrid[0].size();
        
        unsigned int dp[row][col];
        if(obstacleGrid[0][0] == 1 || obstacleGrid[row-1][col-1] == 1) return 0;//起/终点放置障碍物

        dp[0][0] = 1;
        for(int i = 1 ; i < col ; i++){
            obstacleGrid[0][i] == 1 ? dp[0][i] = 0 : dp[0][i] = dp[0][i-1];
        }
        for(int i = 1 ; i < row ; i++){
            obstacleGrid[i][0] == 1 ? dp[i][0] = 0 : dp[i][0] = dp[i-1][0];
        }

        for(int i = 1 ; i < row ; i++){
            for(int j = 1 ; j < col ; j++){
                obstacleGrid[i][j] == 1 ? dp[i][j] = 0 : dp[i][j] = dp[i][j-1] + dp[i-1][j];
            }
        }
        return dp[row-1][col-1];
    }
};

4,测试结果

个人感觉空间复杂度估算应该没问题,但是O(n)运行结果与如下:

然而占用空间却比这位仁兄的多,时间也。。。

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m = obstacleGrid.size();
        if (m < 1) return 0;
        int n = obstacleGrid[0].size();
        if (n < 1) return 0;
        long dp[m][n];  // 使用int提交出现溢出错误,就改为long
        if (1 == obstacleGrid[0][0]) return 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (0 == i && 0 == j) {     //上面判断过(0,0)为1的情况了,这里必定是没有障碍物,因此路径为1
                    dp[i][j] = 1;
                } else if (0 == i && j != 0) {  // 最上面一行网格,如果该点是障碍物,则这一点必定不可达,否则路径和达到其左侧的路径一样
                    dp[i][j] = (1 == obstacleGrid[i][j] ? 0 : dp[i][j - 1]);
                } else if (0 != i && j == 0) { // 最左侧一列网格,如果该点是障碍物,则这一点必定不可达,否则路径和达到其上侧的路径一样
                    dp[i][j] = (1 == obstacleGrid[i][j] ? 0 : dp[i - 1][j]);
                } else {    // 对于坐标均不为0的点,仅在该点为障碍物的时候不可达
                    dp[i][j] = (1 == obstacleGrid[i][j] ? 0 : dp[i][j - 1] + dp[i - 1][j]);
                }
            }
        }
        return static_cast<int>(dp[m - 1][n - 1]);
    }
};

作者:wf0312
链接:https://leetcode-cn.com/problems/unique-paths-ii/solution/dong-tai-gui-hua-duo-yu-yan-zong-you-yi-kuan-gua-h/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值