DP:poj1163 The Triangle

[转]http://old.blog.edu.cn/user3/dongshan/archives/2006/1466731.shtml

The Triangle Time Limit:1000MS  Memory Limit:10000KTotal Submit:3394 Accepted:1971
Description
7
3   8
8   1   0
2   7   4   4
4   5   2   6   5

(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the
highest sum of numbers passed on a route that starts at the top and
ends somewhere on the base. Each step can go either diagonally down to
the left or diagonally down to the right.
 
InputYour program is to read from standard input. The first line contains one
integer N: the number of rows in the triangle. The following N lines describe the
data of the triangle. The number of rows in the triangle is > 1 but <= 100.
The numbers in the triangle, all integers, are between 0 and 99.
OutputYour program is to write to standard output. The highest sum is written as an integer.
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
 
Sample Output
30
 
数字三角形问题,最基本的DP问题,由于题目只要求求出最大和值,这里不需要额
外的空间开销来存储中间值,在从第n-1行往第一行算时可以直接根据前面的值来
确定由这一行的每个值到最后一行最大和值,然后根据此和值覆盖掉址行的数据即可。
在算由第i行的第j个元素到第n行的最大值时,根据第i+1行的第j与j+1个元素值,
a(i,j)=a(i,j)+max(a(i+1,j),a(i+1,j+1))即可。
求解过程:
7
3   8
8   1   0
2   7   4   4
4   5   2   6   5
第一步:
7
3   8
8   1   0
7   12  10  10  从这一行开始,根据下一行值确定此行最大和
第二步:
7
3   8
20  13  10
7   12  10  10
4   5   2   6   5

第三步:
7
23   21
20  13  10
7   12  10  10
4   5   2   6   5

第四步:

30 <----------  (最大和值7+23)
23   21
20  13  10
7   12  10  10
4   5   2   6   5

 

C程序:
#i nclude<stdio.h>
main()
{
 int n,a[101][101],i,j;
 scanf("%d",&n);
 for(i=1;i<=n;i++)
   for(j=1;j<=i;j++)scanf("%d",&a[i][j]);
 for(j=n-1;j>=1;j--)
  for(i=1;i<=j;i++)
   if(a[j+1][i]>a[j+1][i+1])a[j][i]+=a[j+1][i];
   else a[j][i]+=a[j+1][i+1];
 printf("%d",a[1][1]);
}

IEEE Access投稿全流程解析:从初次提交到最终接收的完整指南 本文详细解析了IEEE Access期刊从初次投稿到最终接收的全流程,包括ORCID注册、论文投稿七步法、回稿再投流程及最终版提交等关键步骤。特别强调了4-6周快速同行评审的特点,为研究者提供了一份实用的投稿指南,帮助高效完成IEEE Access期刊投稿。 阅读详情

相关推荐

STC8H硬件IIC从机模式实战:手把手教你驱动OLED屏幕(附完整代码)

本文详细介绍了如何使用STC8H单片机的硬件IIC从机模式驱动SSD1306 OLED屏幕,包括硬件连接、寄存器配置、中断处理和实际应用中的优化技巧。通过完整的代码示例和实战经验分享,帮助开发者快速掌握I2C通信协议和OLED屏幕驱动技术,提升嵌入式开发效率。

weixin_33747129的博客 336

POJ 1163: The Triangle(dp)

【原题】 Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagona

sunyueqinghit的博客 282

Homebrew 必备软件清单:我的 macOS 开发环境搭建全记录

本文详细介绍了如何使用Homebrew在macOS上搭建高效的开发环境,包括基础配置、核心开发工具链、高效编码工具集等30个必备工具。通过Homebrew这一macOS缺失的包管理器,开发者可以一键安装命令行工具和GUI应用,大幅提升工作效率。文章还提供了日常维护命令和疑难问题解决方案,适合从新手到资深开发者的所有用户。

weixin_28836507的博客 353

POJ 1163 The Triangle (简单 DP 数字的最大路线和)

题目大意:从三角形顶部数字走,每次只能走到这个数字的左下角或者右下角的数字,直到底部,计算走过的线路的数字之和,求这个和的最大值。 #include #include #include using namespace std; const int MAXN = 105; int dp[MAXN][MAXN], a[MAXN][MAXN]; int main() {

王老大 716

集成电路模拟版图入门-版图基础学习笔记(四)

第三部分:版图的准备 2. 设计规则 3) Design Rule 2. 设计规则 4) 规则定义 4.1 NW(N WELL) 2. 设计规则 4) 规则定义 4.1 NW(N WELL) 2. 设计规则 4) 规则定义 4.2 PO(Poly) 2. 设计规则 4) 规则定义 4.3 M1(Metal1) 2. 设计规则 4) 规则定义 4.4 VIA 3. DRC文件 3.1 DRC:Design Rule Check,设计规则检查。 3.2 DRC程序了解有关

weixin_52955333的博客 6737

Mac用户看过来!M1/M2芯片安装CiteSpace完整指南(从Homebrew到成功运行)

本文为Mac M1/M2芯片用户提供CiteSpace安装的完整指南,从Homebrew安装适配的Java环境到最终成功运行CiteSpace。详细步骤包括环境检查、Java配置、CiteSpace下载与优化,解决常见问题如内存分配、图形渲染和中文显示乱码,确保科研人员能顺利使用这一文献可视化工具。

weixin_42530458的博客 308

POJ 1163 The Triangle

POJ 1163 The Triangle 题目链接 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on

旺崽的博客 2662

SAP 批次管理

批次管理: 业务场景: 批次常用于:采购、生产、SD等: 1、先进先出,先入库的先出库(如何实现),基于入库时间来自动确定出库策略。 2、追塑:基于批次进行产品质量追溯 例如订单MTO ---> PP ----> MM(批次关联)。SO—>PP工单 --> 预留 --> 发料(基于批次A\B\C)-->产成品入库-->批次入库D--> ...

willieyuan的博客 1万+

POJ1163 The Triangle【DP】

The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 58810 Accepted: 35304 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangl...

海岛Blog 750

Java SSH框架学习

Java SSH框架学习

私信可回答各种问题~ 1792

SAP 物料主数据 Forecasting 视图,别把预测只当成一个数字字段

摘要(150字): SAP S/4HANA物料主数据的Forecasting视图常被忽视,但对依赖历史消耗规律的物料至关重要。该功能基于工厂维度的历史消耗数据,结合预测参数自动计算未来需求,动态调整重订货点和安全库存。预测模板(Forecast Profile)能标准化参数设置,而消耗数据(Consumption Values)的质量直接影响预测结果。预测值作为MRP输入,需注意保存操作才能生效。自动重订货点功能通过分析消耗波动和服务水平,实现库存策略的动态优化。Individual Forecast功能则

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1436

POJ 1163 The Triangle DP

题目:题目链接 题目描述: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that s...

wanglj的博客 360

华为OD技术面八股文_C++_02

汇总华为OD面试C++方面计数八股文真题,提供详细解答。

qq_45776114的博客 767

poj 1163 The Triangle

poj 1163    The Triangle    题目链接:http://poj.org/problem?id=1163 简单DP 题目大意:经典数字三角形 题目分析:状态转移方程 a[ i ] [ j ]+=a[ i+1 ] [ j ]>a[ i+1 ] [ j+1 ]?a[ i+1 ] [ j ]:a[ i+1 ] [ j+1 ](i 递减,j从0递增)为核心算法。 以下是代码:

slicer的技术交流栈 727

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

poj1163The Triangle(DP)

题目意思: 给出一个数字三角形,计算从头走到尾的数字之和的最大值。规定只能向下向右下走。 http://poj.org/problem?id=1163 题目分析: 简单DP,动态转化方程:dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+a[i][j].从下到上进行dp AC代码: #include using namespace std; i

机器笨猫 828

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

【递推DP】POJ1163The Triangle

题目链接:http://poj.org/problem?id=1163 其实这个题目有很多解法,但是我们可以看下这个用一位数组的高效动规解法,这个我上课时老师讲的,很不错; 先保存最后一行4 5 2 6 5,然后更新最大值7=max(4,5)+2;依次类推,很显然,我们就可以得到状态转移方程:dp[j]=max(dp[j],dp[j+1])+a[i][j]; 这样这个题目就很好解决了;

wlxsq的专栏 824

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

POJ-1163(The Triangle)动态规划基础题

7 3 8 8 1 0 2 7 4 4 4 5 2 6 5(Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top an

henuzxy 1719

POJ 1163 DP 动态规划

POJ 1163:The Triangle

陈小轩love 498

基础DP-POJ1163 The Triangle

题目: http://poj.org/problem?id=1163 Description: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that s.

zjsru_Beginner的博客 202

The Triangle(POJ - 1163)

The Triangle POJ - 1163 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a...

the blog of Lee 266

poj 1163 The Triangle

poj 1163 TheTriangle 题目描叙:从定点到最下端,经过的路径和的最大值。 解题思路:dp。细节见代码。 #include #include #include #include using namespace std; int n,a[110][110],dp[110][110]; int main() { int i,j; while(scanf("%

进击的大角虫 324

POJ-1163-The Triangle

链接:https://vjudge.net/problem/POJ-1163 题意: 给一个三角形,每次只能从点往下一层左边或者右边走。 求走到最下面一层能得到的最大值。 思路: dp,每个位置是上方左右中较大的值加上自己。 代码: #include <iostream> #include <memory.h> #include <vector&...

weixin_34018169的博客 114

POJ1163 The Triangle

POJ1163 The Triangle

htrdchh的博客 210

【原】 POJ 1163 The Triangle 三角形最大路径 动态规划 解题报告

http://poj.org/problem?id=1163 方法: DP:将大问题转化为小问题解决。由递归写循环。 c[i][j]表示从a[1][1]开始到a[i][j]的最大和。而最终的结果为c[n][1...n]中的最大值 递归式为: c[1][1] = a[1][1] c[i][j] = max{ c[i-1][j-1], c[i-1][...

ajiaxi111111的博客 167
上一篇: O(logn)求Fibonacci数列
下一篇: DP:poj1579 Function Run Fun
caoruntaogmail
博客等级 码龄18年 3粉丝 302原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值