⭐算法OJ⭐矩阵的相关操作【动态规划 + 组合数学】(C++ 实现)Unique Paths 系列

文章目录

62. Unique Paths

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

复制代码
Input: m = 3, n = 7
Output: 28

Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down

动态规划

思路

  • 定义一个二维数组 dp,其中 dp[i][j] 表示从起点 (0, 0) 到点 (i, j) 的路径数。

  • 机器人只能向右或向下移动,因此状态转移方程为: dp[i][j] = dp[i - 1][j] + dp[i][j - 1]

  • 初始化:第一行和第一列的所有格子只有一种路径(只能一直向右或一直向下),因此 dp[0][j] = 1dp[i][0] = 1

实现代码

cpp 复制代码
int uniquePaths(int m, int n) {
    // 定义 dp 数组
    vector<vector<int>> dp(m, vector<int>(n, 1));

    // 填充 dp 数组
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
        }
    }

    return dp[m - 1][n - 1];
}

复杂度分析

  • 时间复杂度:O(m * n),需要填充整个 dp 数组。
  • 空间复杂度:O(m * n),需要一个二维数组存储中间结果。

组合数学

思路

  • 从起点 (0, 0) 到终点 (m - 1, n - 1),机器人需要移动 m - 1 次向下和 n - 1 次向右。
  • 总移动次数为 (m - 1) + (n - 1) = m + n - 2
  • 问题转化为从 m + n - 2 次移动中选择 m - 1 次向下(或 n - 1 次向右)的组合数。
  • 组合数公式为:C(m + n - 2, m - 1) = (m + n - 2)! / ((m - 1)! * (n - 1)!)

实现代码

cpp 复制代码
int uniquePaths(int m, int n) {
    // 计算组合数 C(m + n - 2, m - 1)
    long long result = 1;
    int total = m + n - 2; // 总移动次数
    int k = min(m - 1, n - 1); // 选择较小的值计算组合数

    for (int i = 1; i <= k; i++) {
        result = result * (total - k + i) / i;
    }

    return (int)result;
}

复杂度分析

  • 时间复杂度:O(min(m, n)),只需要计算组合数。
  • 空间复杂度:O(1),只使用了常数空间。

63. Unique Paths II

You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle.

Return the number of possible unique paths that the robot can take to reach the bottom-right corner.

Example 1:

复制代码
Input: obstacleGrid = [[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

Example 2:

复制代码
Input: obstacleGrid = [[0,1],[0,0]]
Output: 1

这个问题是带障碍物的机器人路径问题,可以通过动态规划来解决。我们需要考虑障碍物对路径的影响,并在动态规划的过程中跳过障碍物。

动态规划

定义状态

dp[i][j] 表示从起点 (0, 0) 到点 (i, j) 的路径数。

状态转移方程

  • 如果 grid[i][j] == 1(障碍物),则 dp[i][j] = 0,因为无法通过障碍物。
  • 否则,dp[i][j] = dp[i - 1][j] + dp[i][j - 1],即从上方或左方到达当前点的路径数之和。

初始化

  • 如果起点 (0, 0) 是障碍物,则直接返回 0,因为无法开始移动。
  • 初始化第一行和第一列:
    • 如果某个格子是障碍物,则它及其后续格子的路径数都为 0。
    • 否则,路径数为 1(只能一直向右或一直向下)。
cpp 复制代码
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
    int m = obstacleGrid.size(), n = obstacleGrid[0].size();
    vector<vector<int>> dp(m, vector<int>(n, 0));
    dp[0][0] = 1;
    for (int i = 1; i < m; i++) {
        if (obstacleGrid[i][0] == 1) {
            dp[i][0] = 1;
        }
        else {
            dp[i][0] = dp[i - 1][0];
        }
    }
    for (int j = 1; j < n; j++) {
        if (obstacleGrid[0][j] == 1) {
            dp[0][j] = 0;
        }
        else {
            dp[0][j] = dp[0][j - 1];
        }
    }
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (obstacleGrid[i][j] == 1) {
                dp[i][j] = 0;
            }
            else {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }  
        }
    }
    return dp[m - 1][n - 1];
}

复杂度分析

  • 时间复杂度:O(m * n),需要遍历整个网格。
  • 空间复杂度:O(m * n),需要一个二维数组存储中间结果。

优化空间复杂度

我们可以将空间复杂度优化为 O(n),因为每次更新 dp[i][j] 时只需要用到当前行和前一行的数据。

状态转移方程

对于每一行 i,从左到右更新 dp[j]dp[j] = dp[j] + dp[j - 1]

  • dp[j] 表示从上方来的路径数(即上一行的 dp[j])。
  • dp[j - 1] 表示从左方来的路径数(即当前行的 dp[j - 1])。
cpp 复制代码
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
    int m = obstacleGrid.size();
    int n = obstacleGrid[0].size();
    if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) {
        return 0;
    }
    vector<int> dp(n, 0);
    dp[0] = 1;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (obstacleGrid[i][j] == 1) {
                dp[j] = 0;
            } else if (j > 0) {
                dp[j] += dp[j - 1];
            }
        }
    }
    return dp[n - 1];
}
相关推荐
机器学习之心1 小时前
多目标鲸鱼优化算法(NSWOA),含46种测试函数和9个评价指标,MATLAB实现
算法·matlab·多目标鲸鱼优化算法·46种测试函数·9个评价指标
max5006002 小时前
基于Meta Llama的二语习得学习者行为预测计算模型
人工智能·算法·机器学习·分类·数据挖掘·llama
王哥儿聊AI3 小时前
Lynx:新一代个性化视频生成模型,单图即可生成视频,重新定义身份一致性与视觉质量
人工智能·算法·安全·机器学习·音视频·软件工程
手握风云-5 小时前
优选算法的寻踪契合:字符串专题
算法
闭着眼睛学算法5 小时前
【华为OD机考正在更新】2025年双机位A卷真题【完全原创题解 | 详细考点分类 | 不断更新题目 | 六种主流语言Py+Java+Cpp+C+Js+Go】
java·c语言·javascript·c++·python·算法·华为od
IT古董5 小时前
【第五章:计算机视觉-项目实战之目标检测实战】2.目标检测实战:中国交通标志检测-(2)中国交通标志检测数据格式转化与读取
算法·目标检测·计算机视觉
MobotStone5 小时前
LLM 采样入门到进阶:理解与实践 Top-K、Top-P、温度控制
算法
杨小码不BUG6 小时前
CSP-J/S初赛知识点精讲-图论
c++·算法·图论··编码·csp-j/s初赛
LeaderSheepH7 小时前
常见的排序算法
数据结构·算法·排序算法
青云交8 小时前
Java 大视界 -- Java 大数据在智能公交调度优化与准点率提升中的应用实践(416)
java·动态规划·flink cep·spark mllib·智能公交调度·杭州公交案例·准点率提升