AcWing 1027. 方格取数【动态规划】【线性DP】【数字三角形】

【动态规划专题】数字三角形模型 题目 算法 AcWing 1015. 摘花生 简单线性DP、数字三角形模型 AcWing 1018. 最低通行费 数字三角形模型 AcWing 1027. 方格取数 DP AcWing 275. 传纸条 DP 闫氏DP分析法 从集合的角度思考DP问题 从最基础的例题开始分析 A、AcWing 1015. 摘花生 例题分析: #include<iostream> #include<cstdio> #include<algorithm> #... 阅读详情


一、题目链接

AcWing 1027. 方格取数


二、题目分析

(一)算法标签

动态规划 线性DP 数字三角形

(二)解题思路

只有当i1 + j1 == i2 + j2时,两条路径的格子才可能重合
因此用k表示i1 + j1和i2 + j2时,f[k][i1][i2]表示所有从(1, 1),(1, 1)分别走到(i1, k - i1)和(i2, k - i2)的路径的最大值


三、AC代码

解法一:

#include <iostream>

using namespace std;

const int N = 15;

int n, a, b, c;
int f[N * 2][N][N], w[N][N];

int main()
{
    scanf("%d", &n);
    
    while (cin >> a >> b >> c, a || b || c)
        w[a][b] = c;
        
    for (int k = 1; k <= n + n; k ++ )
        for (int i1 = 1; i1 <= n; i1 ++ )
            for (int i2 = 1; i2 <= n; i2 ++ )
            {
                int j1 = k - i1, j2 = k - i2;
                if (j1 >= 1 && j1 <= n && j2 >= 1 && j2 <= n)
                {
                    
                    int &x = f[k][i1][i2];
                    int t = w[i1][j1];
                    if (i1 != i2) t += w[i2][j2]; 
                    x = max(x, f[k - 1][i1 - 1][i2 - 1] + t);
                    x = max(x, f[k - 1][i1 - 1][i2] + t);
                    x = max(x, f[k - 1][i1][i2 - 1] + t);
                    x = max(x, f[k - 1][i1][i2] + t);
                }
            }
    printf("%d",f[n + n][n][n]);
    
    return 0;
}

四、其它题解

AcWing 1027. 方格取数

本体(Ontology)概述 认识本体        本体(Ontology)的概念源自于哲学领域,在哲学中的定义为“对世界上客观事物的系统描述,即存在论”。哲学中的本体关心的是客观现实的抽象本质。而在计算机领域,本体可以在语义层次上描述知识,可以看成描述某个学科领域知识的一个通用概念模型。德国学者Studer在1998年给出了本体的相关定义“本体是共享概念模型的形式化规范说明”。这个定义包含了四层含义:即共享(share)... 阅读详情

相关推荐

从网格划分到端口接地:一份给ADS新手的Momentum RF仿真避坑实操指南

本文为ADS新手提供Momentum RF仿真的实用指南,涵盖网格划分、端口接地、特殊结构处理等关键设置技巧。通过30-50 cells/λ原则、1/10波长限制等黄金法则,帮助用户平衡仿真精度与效率,避免常见错误,提升电磁仿真结果的准确性。

weixin_33692284的博客 509

【算法|动态规划 | 线性dp | 数字三角形模型No.1】AcWing1015.摘花生 & AcWing898.数字三角形 & AcWing1027.方格取数 & AcWing275.传纸条

【算法|动态规划 | 线性dp | 数字三角形模型No.1】AcWing1015.摘花生 & AcWing898.数字三角形 & AcWing1027.方格取数 & AcWing275.传纸条

m0_74352571的博客 338

STM32+HX711实战:5分钟搞定拉力传感器数据采集(附完整代码)

本文详细介绍了如何使用STM32微控制器和HX711模数转换器快速搭建高精度拉力测量系统。通过硬件连接指南、驱动实现代码和校准方法,帮助开发者在5分钟内完成拉力传感器数据采集,适用于工业自动化和消费电子等场景。

y7z8a9的博客 883

AcWing 1027. 方格取数 (线性dp + 数字三角形模型)

1027. 方格取数 题意 设有 N×NN×NN×N 的方格图,我们在其中的某些方格中填入正整数,而其它的方格中则放入数字0。如下图所示: 某人从图中的左上角 A 出发,可以向下行走,也可以向右行走,直到到达右下角的 B 点。 在走过的路上,他可以取走方格中的数(取走后的方格中将变为数字0)。 此人从 A 点到 B 点共走了两次,试找出两条这样的路径,使得取得的数字和为最大。 思路 dpdpdp 问题 最初想法 设 dp[i1][j1][i2][j2]dp[i1][j1][i2][j2]dp[i1][j1

zzqwtc的博客 3351

03、ZigBee 开发教程之进阶篇—CC2530无线传输质量检测

文章目录1、Packet Error Rate Tester(包错误率测试)理论分析1.1、工作流程1.2、流程图1.3、丢包、PER、RSSI计算方法2、实验详解2.1、实验目的2.2、实验设备2.3、实验现象2.4、源码分析2.5、实验步骤及现象 1、Packet Error Rate Tester(包错误率测试)理论分析 1.1、工作流程 Packet Error Rate Tester(包错误率测试) 1、启动 板载外设、射频IO、系统时钟、中断等初始化(halBoardInit();); Bas

嵌入式羊圈 1547

线性DP-数字三角形模型

这里写目录标题题目关系图Acwing898 数字三角形输入格式输出格式数据范围输入样例:输出样例:胜利曙光AC:Acwing1015 摘花生输入格式输出格式数据范围输入样例:输出样例:胜利曙光AC:Acwing1018 最低通行费输入格式输出格式数据范围输入样例:输出样例:样例解释胜利曙光AC:Acwing1027 方格取数输入格式输出格式数据范围输入样例:输出样例:胜利曙光AC:Acwing 275 传纸条输入格式输出格式数据范围输入样例:输出样例:胜利曙光AC: 题目关系图 Acwing898 数字

qq_52384627的博客 1546

Android Telephony主要模块及功能

Telephony的代码主要由五个模块组成 1.Telephony Frameworks 代码区域: frameworks/opt/telephony/ 其中主要的文件在如下目录 /frameworks/opt/telephony/src/java/com/android/internal/telephony/ 其中比较常见的有以下几类文件 1.BaseCommands、RIL构成R

泊客 7741

Acwing算法—动态规划

目录 数字三角形模型AcWing 898. 数字三角形AcWing 1015. 摘花生AcWing 1018. 最低通行费AcWing 1027. 方格取数AcWing 275. 传纸条最长上升子序列模型AcWing 895. 最长上升子序列AcWing 896. 最长上升子序列 IIAcWing 1017. 怪盗基德的滑翔翼AcWing 1014. 登山AcWing 482. 合唱队形AcWing 1012. 友好城市AcWing 1016. 最大上升子序列和AcWing 1010. 拦截导弹AcWing

HENRYyangtuo的博客 5625

Claude Code 分布式并行开发最佳实践:1中枢+10Worker跨多Git仓库全流程落地

本文介绍了一套基于Claude Code和Git Worktree的分布式并行开发方案,解决多仓库、微服务开发中的核心痛点。方案采用"1个决策中枢+10个并行Worker"架构,中枢负责任务拆解和调度,每个Worker绑定独立仓库实现完全隔离开发。关键优势包括:任务并行效率提升10倍、跨仓库依赖自动编排、版本统一管理、零第三方依赖。文章详细提供了环境准备、架构设计、中枢搭建和初始化脚本等全流程落地指南,特别适合多仓库协同的全栈开发场景。

AI天才研究院 248

【Docker】常见容器问题解决#OCI runtime create failed: container_linux.go:349: starting container process

ERROR: for nginx Cannot start service nginx: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"rootfs_linux.go:58: mounting \\\"/data/docker-compose/n

liu_chen_yang的博客 5538

数字三角形模型(动态规划)

数字三角形模型,acwing算法提高课动态规划

一起刷题一起进步昂 585

基于FPGA的以太网设计(1)----大白话解释什么是以太网

基于FPGA的千兆以太网设计(1)----大白话解释什么是以太网

孤独的单刀 3894

AcWing 1027. 方格取数(数字三角形线性DP)

算法思想: 四维未优化版 import java.util.*; class Main{ static int n = 0, N = 15; static int[][] nums = new int[N][N]; static int[][][] max = new int[N + N][N][N]; public static void main(String[] args)throws Exception{ Scanner sc ...

StrawberryMuMu的博客 328

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

acwing 1027 方格取数 (数字三角形模型)

题面 题解(数字三角形模型) 线性DP ,数字三角形模型,摘花生题拓展 ,我们知道,如果是从A到B走一次,那么直接 f[i] [j] = max( f[i-1] [j] , f[i] [j-1] ) + w[i] [j] 考虑坐标为(i,j)的点从上边还是从右边转移过来的最大值即可,但是这个题是要求走两次最大,而且第一次走过的点值会变为0,当然我们也不可以分别求两次,因为两路径最大值相加不一定等于两路径相加的最大值 我们可以同时走两条路 f[i1][j1][i2][j2] 就表示所有从(1,

qq_44791484的博客 214

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

2024-02-10(动态规划-数字三角形模型)

由于题目限制了商人必须在2n - 1的时间内走出去,也就是不走回头路,就是说只能往右走或者往下走,其实本质上和摘花生这个题是一样的。题目中要求从左上到右下,和右下到左上,其实都可以看作是。然后再考虑一下边界情况就可以了。只有在步数一样的时候才有可能踩到相同的格子。题目本质上和方格取数是一样的,只是矩阵不一样。

m0_73165551的博客 450

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ACM - DP - 提高1 (数字三角形 / 方格 + 最长上升子序列 + 背包)

这里写目录标题一、数字三角形模型:只能朝右或下走1、AcWing 1018. 最低通行费:只走一次2、AcWing 1027. 方格取数:走两次,只拿一次,且可重复3、AcWing 275. 传纸条:走两次,只拿一次,且不可重复二、最长上升子序列模型1、AcWing 1014. 登山:既上山又下山2、AcWing 1012. 友好城市:线性互不交叉3、AcWing 1016. 最大上升子序列和4、AcWing 1010. 拦截导弹:最长上升子序列 == 最少用多少个非上升子序列才能填满整个序列(反之亦然)5

CSDNWudanna的博客 676

【AcWing算法提高课】1.1数字三角形模型

AcWing算法提高课 1.1数字三角形模型

m0_71477690的博客 247

Day2 898.数字三角形(线性DP)

从集合角度分析DP问题(课程笔记,来自y总的思想) AC代码 #include<iostream> #include<algorithm> #include<math.h> using namespace std; const int N = 510; int main(){ int n,arr[N][N],dp[N][N]; ///n--数目 arr--存数字三角形 dp--dp数组 cin>>n; for(

z1592415023的博客 139

AcWing我觉得需要反复品味思路的题

xin007hoyo的博客 48

常用算法代码模板与题单 | E - 动态规划(更新于2026.10.7)

这篇文档其实是我自己学习路上的一份“复习笔记”。之前看了不少教程,零零散散记了一堆代码片段和小技巧,时间久了就容易忘,翻起来也麻烦。所以干脆花时间整理了一下,把常用的模板、容易踩的坑、还有一些个人觉得挺实用的小技巧都归到了一起。 如果你也在学类似的东西,或许能帮你省下一些翻找的时间;如果有哪里写得不清楚或不对,也欢迎随时指正交流。 希望这份整理也能对你有一点点帮助~ ✨

团爸讲算法 302

强化学习进阶篇十四 DPO算法

DPO(Direct Preference Optimization,直接偏好优化)由 Rafailov 等人于 2023 年提出。它用「同一个提示词下,回答 A 比回答 B 更合适」这样的成对偏好数据直接微调语言模型。偏好标签可以来自人工标注,也可以像本文的唐诗示例一样按明确规则构造。与采用 PPO 的经典 RLHF 流程相比,DPO 不必单独训练奖励模型,也不必在微调过程中从当前策略在线采样。它从带 KL 正则的奖励最大化目标出发,把奖励改写为当前策略与冻结参考策略的对数概率比,再用 Bradley–T

338

【三年面试五年模拟】2026-11-08_米哈游AI大模型算法岗面经全解析

米哈游大模型算法岗面经全解析:逐题解析9个问题,覆盖Agent推理与工具调用、轨迹和记忆、奖励排障、DPO长度偏差、学习率退火、游戏GUI Agent评测、RoPE长度扩展及DPO/PPO对比,补充状态恢复、数值算例与实验设计。

Rocky Ding相信人工智能、数据科学、商业逻辑、金融工具、终身成长以及顺应时代的潮流会赋予我们超能力。 236

【三年面试五年模拟】2026-10-21_零一万物知识图谱算法实习生一面面经全解析

一万字【三年面试五年模拟】2026-10-21_零一万物知识图谱算法实习生一面面经全解析

Rocky Ding相信人工智能、数据科学、商业逻辑、金融工具、终身成长以及顺应时代的潮流会赋予我们超能力。 395

智能体训练-模仿学习-DAgger算法详解与实战

DAgger 用「学习者探索 + 专家标注 + 数据聚合」的简洁思路,优雅地解决了行为克隆的分布偏移问题,并从理论上把复合误差从OϵT2OϵT2降到OϵTOϵT。问题定位准确:明确指出 BC 的失败根源是训练与测试状态分布不一致;方法简单有效:只需在策略访问的状态上补充专家标签;理论保证扎实:通过 no-regret 在线学习框架给出收敛性证明。当然,DAgger 的代价是需要在线访问专家,这在很多真实场景中并不便宜。

qq_42773521的博客 350

【算法拾光】拓扑排序|火星词典

拓扑排序用于有向无环图(DAG)的节点排序,核心是求满足依赖关系的执行顺序。在“火星词典”问题中,通过比较相邻单词首差异字符构建字符间先后关系,用Kahn算法进行拓扑排序。关键点:去重边、前缀非法判断(长词排前),若结果长度不等于字符数则存在环,返回空串。答案不唯一,任意合法序列均可。

wuqingyigexiaoba的博客 198
上一篇: AcWing 1018. 最低通行费【动态规划】【线性DP】【数字三角形】
下一篇: AcWing 1017. 怪盗基德的滑翔翼【动态规划】【线性DP】【最长上升子序列】
Derrickhang
博客等级 码龄7年 13粉丝 95原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值