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 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
题解:最基础的dp,直接记录状态
dp[i][j]表示以第i行第j列开头的三角形的最大值
dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+a[i][j];
(1) 从后向前递推
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = ;
int dp[MAXN][MAXN];
int main()
{
int n;
cin >> n;
for (int i = ; i < n;i++)
for (int j = ; j <= i; j++)
cin >> dp[i][j];
for (int i = n - ; i >= ;i--)
for (int j = ; j <= i; j++)
{
dp[i][j] = max(dp[i+][j],dp[i+][j+]) + dp[i][j];
}
cout << dp[][];
return ;
}
(2) 从前向后递推
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = ;
int dp[MAXN][MAXN];
int main()
{
int n;
cin >> n;
for (int i = ; i < n;i++)
for (int j = ; j <= i; j++)
cin >> dp[i][j];
for (int i = ; i < n;i++)
for (int j = ; j <= i; j++)
{
if (i==)continue;
else if (j == )dp[i][j] = dp[i-][j] + dp[i][j];
else if (j == i)dp[i][j] = dp[i-][j-] + dp[i][j];
else dp[i][j] = max(dp[i-][j],dp[i-][j-]) + dp[i][j];
}
int ans = ;
for (int j = ; j < n; j++)
ans = max(ans,dp[n-][j]);
cout << ans;
return ;
}
POJ 1163:The Triangle的更多相关文章
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- 【九度OJ】题目1163:素数 解题报告
[九度OJ]题目1163:素数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1163 题目描述: 输入一个整数n(2< ...
- OpenJudge/Poj 1163 The Triangle
1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...
- POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49955 Accepted: 30177 De ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- POJ 1163 The Triangle 简单DP
看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...
- Poj 1163 The Triangle 之解题报告
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42232 Accepted: 25527 Description 7 3 ...
随机推荐
- jquery实现input输入框实时输入触发事件代码 ---jQuery 中bind(),live(),delegate(),on() 区别
复制代码 代码如下: <input id="productName" name="productName" value="" /> ...
- freemodbus-v1.5.0 源码分析
注:转载请注明出处 http://www.cnblogs.com/wujing-hubei/p/5935142.html FreeModbus协议栈作为从机,等待主机传送的数据,当从机接收到一帧完 ...
- atom 折腾记(转载的)
http://www.bkjia.com/webzh/999078.html
- ios中的RunLoop 和 android 中的Looper
今天写android程序,用到了Handler,晚上回来查阅资料,发现了Looper这个概念. 看了一下网上关于Looper的资料,发现这个Looper跟ios中的Runloop基本的理念完全一致! ...
- 针对跑MySQL的Linux优化【转】
本文来自:http://www.mysqlsupport.cn/linux-performance-tuning-tips-mysql/ 因为很多MySQL的生产环境都在Linux下,我决定指出一些L ...
- Django~automated tests
def xx(): 冒号下一行要缩进 ATD http://blog.csdn.net/doupei2006/article/details/7657547 http://www.jb51.net/a ...
- nyoj221_Tree_subsequent_traversal
Tree 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Little Valentine liked playing with binary trees very ...
- html中的alt和title用法区别
html中的alt和title用法区别 首先明确一下概念,alt是html标签的属性,而title既是html标签,又是html属性.title标签这个不用多说,网页的标题就是写在<title& ...
- 【linux】ubuntu stmp服务器配置
来源:http://blog.itpub.net/786540/viewspace-1002077/ sudo apt-get install sendmail(其中已经包含了sendmail-bin ...
- xcode运行push通知总是提示输入用户名和密码
xcode运行push通知总是提示输入用户名和密码,目前找到的解决方案是, 打开钥匙串,然后找到push证书下面的私有密钥,双击进入将访问控制设置为允许全部访问,即可.