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;
}
相关推荐
Yungoal几秒前
C++基础语法3
开发语言·c++
今儿敲了吗2 分钟前
python基础学习笔记第四章
c++·笔记·python·学习
像污秽一样5 分钟前
算法设计与分析-习题9.1
数据结构·算法·dfs·dp·贪婪
無限進步D10 分钟前
差分算法 cpp
c++·算法·蓝桥杯·竞赛
biter down18 分钟前
C++ 精准控制对象的创建位置(堆 / 栈)
开发语言·c++
星轨初途18 分钟前
类和对象(上)
开发语言·c++·经验分享·笔记
留院极客离心圆26 分钟前
C++ 进阶笔记:栈内存 vs 堆内存
开发语言·c++
留院极客离心圆27 分钟前
C++ 进阶笔记:宏
开发语言·c++·笔记
星空露珠31 分钟前
迷你世界UGC3.0脚本Wiki生物模块管理接口 Monster
开发语言·数据结构·算法·游戏·lua
星空露珠31 分钟前
迷你世界UGC3.0脚本Wiki世界模块管理接口 World
开发语言·数据库·算法·游戏·lua