POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】
The Triangle
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 49955 | Accepted: 30177 |
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.
Input
Output
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
Source
输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线。
规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个数中的一个。
题目分析:
典型的动态规划。
因此我们可以从下往上推,相邻的两个数中找较大的与上层相加,得出的结果相邻的两个数中再找较大的与上层相加,以此类推。用二维数组d_[][]记录从下到该点的最大值。
核心代码
d[i][j] += d[i+1][j] > d[i+1][j+1] ? d[i+1][j] : d[i+1][j+1];
最后的结果就是d[0][0]。
下面给出AC代码:
#include <stdio.h>
#include <string.h>
int max(int a,int b)
{
return a>b?a:b;
}
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
{
write(x/);
}
putchar(x%+'');
}
int a[][],d[][];
int n;
int dp(int i,int j)
{
if(d[i][j]>=)
return d[i][j];
return d[i][j]=a[i][j]+(i==n-?:max(dp(i+,j),dp(i+,j+)));
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
for(j=;j<i+;j++)
scanf("%d",&a[i][j]);
memset(d,-,sizeof(d));
printf("%d\n",dp(,));
}
return ;
}
POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】的更多相关文章
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- [noip2016]组合数问题<dp+杨辉三角>
题目链接:https://vijos.org/p/2006 当时在考场上只想到了暴力的做法,现在自己看了以后还是没思路,最后看大佬说的杨辉三角才懂这题... 我自己总结了一下,我不能反应出杨辉三角的递 ...
- poj 1163 The Triangle(dp)
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43993 Accepted: 26553 De ...
- 每天一道LeetCode--118. Pascal's Triangle(杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- POJ 1163 The Triangle DP题解
寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
随机推荐
- Android 开发,你遇上 Emoji 头疼吗?
在 Android 中,如果需要使用的到 Emoji 表情,你会发现在某些设备上,有一些 Emoji 表情会被以豆腐块 "☐" 的形式显示,这是因为当前设备并不支持这个 Emoji ...
- Boost Coroutine2 - stackful coroutine简介
协程可以很轻量的在子例程中进行切换,它由程序员进行子例程的调度(即切换)而不像线程那样需要内核参与,同时也省去了内核线程切换的开销,因为一个协程切换保留的就是函数调用栈和当前指令的寄存器,而线程切换需 ...
- C# TreeView 控件的综合使用方法
1.概述 该篇文章开发使用的语言c#,环境visualstudio2010,sql数据库.主要内容包括: (1)treeView控件添加根节点.子节点的基本方法,节点的删除. (2)把treeView ...
- HDFS租约实践
一.租约详解 Why租约 HDFS的读写模式为 "write-once-read-many",为了实现write-once,需要设计一种互斥机制,租约应运而生租约本质上是一个有时间 ...
- 大白话Vue源码系列(05):运行时鸟瞰图
阅读目录 Vue 实例的生命周期 实例创建 响应的数据绑定 挂载到 DOM 节点 结论 研究 runtime 一边 Vue 一边源码 初看 Vue 是 Vue 源码是源码 再看 Vue 不是 Vue ...
- scrapy框架第一章
操作环境:python2.7+scrapy 安装比较简单,网上教程也超多,就不在此赘述. 示例网站:https://www.cnblogs.com/cate/python/ (爬去关于博客园所有pyt ...
- java inputstream to string
https://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string 过千赞的答案
- SQLServer Agent执行[分发清除: distribution] 无法删除快照文件
由于之前创建的发布订阅造成严重的性能压力,症状表现为发布订阅表查询产生CMEMTHREAD suspend等待,由于开发配置每隔十分钟会产生大量的SQLCOMMAND(create table,cr ...
- vue2.0 微信oauth认证的正确调用位置
运行在微信端的项目,很重要的环节是oauth认证:那在vue项目中,在何时何地调用oauth认证最合适呢? 经过观察,在项目启动过程中,会执行main.js文件:所以我将认证放在main.js中操作: ...
- css实现连续的图像边框
有时我们想把一个图片应用为边框,而不是背景,最简单的办法是使用两个HTML元素,一个元素用来把我们的石雕图片设置为背景,另一个元素用来存放内容,并设置纯白背景,然后覆盖在前者之上,这个方法需要一个额外 ...