29.路径类dp

一.矩阵的最小路径和

题目链接:https://www.nowcoder.com/practice/38ae72379d42471db1c537914b06d48e?tpId=230&tqId=39755&ru=/exam/oj

1.题目解析

2.代码实现

cpp 复制代码
#include <iostream>
#include <cstring>

using namespace std;

const int N = 510;
int n,m;
int f[N][N];

int main()
{
    cin >> n >> m;
    memset(f,0x3f,sizeof(f));
    f[0][1] = 0;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)
        {
            int t;
            cin >> t;
            f[i][j] = min(f[i - 1][j],f[i][j - 1]) + t;
        }
    }
    cout << f[n][m] << endl;
    return 0;
}

二.迷雾森林

题目链接:https://ac.nowcoder.com/acm/problem/53675

​

​

1.题目解析

​

​

2.代码实现

cpp 复制代码
#include <iostream>

using namespace std;

const int N = 3010, MOD = 2333;
int n,m;
int a[N][N];
int f[N][N];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    f[n][0] = 1;
    for(int i = n;i >= 1;i--)
    {
        for(int j = 1;j <= m;j++)
        {
            if(a[i][j] == 0) f[i][j] = (f[i + 1][j] + f[i][j - 1]) % MOD;
        }
    }
    cout << f[1][m] << endl;
    return 0;
}

三.过河卒

题目链接:https://www.luogu.com.cn/problem/P1002

​

​

1.题目解析

2.代码实现

四.方格取数

题目链接:https://www.luogu.com.cn/problem/P1004

1.题目解析

2.代码实现

cpp 复制代码
#include <iostream>

using namespace std;
typedef long long LL;
const int N = 25;

int n,m,a,b;
LL f[N][N];

bool check(int x,int y)
{
    return (x == a && y == b) || (x != a && y != b && abs(x - a) + abs(y - b) == 3);
}

int main()
{
    cin >> n >> m >> a >> b;
    n++;m++;a++;b++;
    f[0][1] = 1;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)
        {
            if(check(i,j)) continue;
            f[i][j] = f[i - 1][j] + f[i][j - 1];
        }
    }
    cout << f[n][m] << endl;
    return 0;
}
相关推荐
Pniubi5 小时前
01背包类问题-力扣416.分割等和子集
算法·leetcode·职场和发展
eggwyw15 小时前
C++ union 的一种妙用法
开发语言·c++
罗西的思考16 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
ShineWinsu17 小时前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
小七在进步18 小时前
string的实现(1)
c++·笔记·visual studio
kyle~20 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
敲上瘾20 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
牢姐与蒯21 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好1 天前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
lcj25111 天前
[C++] C++11
开发语言·c++·笔记