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题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
随机推荐
- lodash源码分析之Hash缓存
在那小小的梦的暖阁,我为你收藏起整个季节的烟雨. --洛夫<灵河> 本文为读 lodash 源码的第四篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash gitbo ...
- 中文代码示例之Vuejs入门教程(一)
原址: https://zhuanlan.zhihu.com/p/30917346 为了检验中文命名在主流框架中的支持程度, 在vuejs官方入门教程第一部分的示例代码中尽量使用了中文命名. 所有演示 ...
- ES6(二) Destructuring-变量的解构赋值
1.解构的含义 允许按照一定的模式,从数组和对象中取值,对变量进行赋值,称为解构. 解构赋值时,只要等号右边的值不是对象,就先将其转换成对象. 本质上,这种写法属于 “模式匹配”,只要两边模式相同,左 ...
- zabbix 3.0.4 中文字体替换
zabbix 对中文支持不是很好,会出现乱码: 从windows系统里 找到字体包:如图: 拷贝到zabbix-server里面,注意,把文件名改成小写: 我linux 是centos7.2版本 [r ...
- robotframework的学习笔记(十四)------学习Robot Framework必须掌握的库—-BuiltIn库
作为一门表格语言,为了保持简单的结构,RF没有像别的高级语言那样提供类似if else while等内置关键字来实现各种逻辑功能,而是提供给了用户BuiltIn库.如果用户想在测试用例中实现比较复杂的 ...
- fiddler基本介绍
1.Fiddler如何捕获HTTPS会话 点击Tools->Teleik Fiddler Option,勾选如下选项 点击"Yes" 后,就设置好了 2.fiddler的基本 ...
- 安装Django时报错'module' object has no attribute 'lru_cache'
使用pip方法安装Django时报错'module' object has no attribute 'lru_cache' 解决办法如下 命令行输入命令sudo pip install Django ...
- Check whether a remote server port is open on Linux
链接:https://www.pixelstech.net/article/1514049471-Check-whether-a-remote-server-port-is-open-on-Linux
- screen使用
远程连接Linux系统后,需要在后台运行一下程序,nohup呢感觉不大直观,打心里不信任它..那么screen就是很棒的工具,除了"后台"运行程序,还能做到分屏等等. 下面介绍一下 ...
- 利用TortoiseGit对Coding项目进行版本管理
Git配置: 1),首先去Git官网下载最新的Git,https://git-for-windows.github.io/ 2),下载对应的版本,然后一路next点击安装. Git与Coding联通 ...