leetcode-62.不同路径

1. 题目

2. 解答

dpij表示机器人位于第i,j位置的时候,有多少路径

  1. 如果i = 0,dpij = 1;
  2. 如果j = 0,dpij = 1;
  3. 其他情况dpij = dpi-1j + dpij - 1
c 复制代码
#include <stdio.h>

int solve(int m, int n)
{
    int dp[m][n];

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (i == 0 || j == 0) {
                dp[i][j] = 1;
            } else {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
    }

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

int main()
{
    int m, n;
    scanf("%d %d", &m, &n);

    int result = solve(m, n);

    printf("result:%d\n", result);
}

运行:

c 复制代码
G3-3579:~/data/source/leetcode$ gcc 62differrentpath.c 
G3-3579:~/data/source/leetcode$ ./a.out 
3 7
result:28
G3-3579:~/data/source/leetcode$ ./a.out 
3 2
result:3
G3-3579:~/data/source/leetcode$ ./a.out 
7 3
result:28
G3-3579:~/data/source/leetcode$ ./a.out 
3 3
result:6
相关推荐
洛水水1 分钟前
【力扣100题】81.寻找两个正序数组的中位数
数据结构·算法·leetcode
happymaker062630 分钟前
LeetCodeHot100——155.最小栈
算法
洛水水41 分钟前
【力扣100题】85.每日温度
算法·leetcode·职场和发展
Coder-magician1 小时前
《代码随想录》刷题打卡day15:二叉树part05
数据结构·c++·算法
Kurisu_红莉栖1 小时前
力扣56合并区间
算法·leetcode
Irissgwe1 小时前
算法的时间复杂度和空间复杂度
数据结构·c++·算法·c·时间复杂度·空间复杂度
随意起个昵称1 小时前
区间dp-基础题目3(永别)
c++·算法
周末也要写八哥1 小时前
有向图Hierholzer算法的另一种实现
算法
bIo7lyA8v1 小时前
算法调优中的性能回归与基准测试分析的技术8
算法·数据挖掘·回归
有点。1 小时前
C++贪心算法二(练习题)
c++·算法·贪心算法