Poj 1163 The Triangle 之解题报告
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42232 | Accepted: 25527 |
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
对于这题若按照常规可能做不出来;但是用递推的思想就可以计算出来啦
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; const int maxn = +;
int vis[maxn][maxn];
int Triangle[maxn][maxn]; int max(int a,int b)
{
return a>b?a:b;
} int main(void)
{
int i,j,n;
while(~scanf("%d",&n))
{
for(i=;i<=n;++i)
for(j=;j<=i;++j)
scanf("%d",&Triangle[i][j]);
memset(vis,,sizeof(vis));
for(i=;i<=n;++i)
{
for(j=;j<=i;++j)
{
if(i==) vis[i][j]=Triangle[i][j];
else if(j==) vis[i][j] = vis[i-][j]+Triangle[i][j];
else if(j==i) vis[i][j] = vis[i-][j-]+Triangle[i][j];
else vis[i][j] = max(vis[i-][j]+Triangle[i][j],vis[i-][j-]+Triangle[i][j]);
}
}
int ans = ;
for(i=;i<n;++i)
ans = max(ans,vis[n][i]);
cout<<ans<<endl;
} return ;
}
Poj 1163 The Triangle 之解题报告的更多相关文章
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
- 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(经典问题教你彻底理解动归思想)
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38195 Accepted: 22946 De ...
- POJ 1163 The Triangle 简单DP
看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
随机推荐
- AMH4.2 虚拟主机面板Tengine版本
本人将原版Nginx更换成淘宝的Tengine 2.1.0 并且更换安装源,以保证面板正常安装 AMH4.2 修改版安装 ———————AMH为独立的一套LNMP/Nginx虚拟主机面板 安装请使用纯 ...
- 基于内嵌Tomcat的应用开发
为什么使用内嵌Tomcat开发? 开发人员无需搭建Tomcat的环境就可以使用内嵌式Tomcat进行开发,减少搭建J2EE容器环境的时间和开发时容器频繁启动所花时间,提高开发的效率. 怎么搭建内嵌To ...
- 解决 Eclipse build workspace 慢,validation javascript 更慢的问题
鸣谢:http://zuoming.iteye.com/blog/1430925 ------------------------------------------------ 如果用到js插件或者 ...
- 【数学】[BZOJ 3884] 上帝与集合的正确用法
Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“元” ...
- bzoj 2744: [HEOI2012]朋友圈 二分图匹配
2744: [HEOI2012]朋友圈 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 612 Solved: 174[Submit][Status] ...
- spoj 42
简单题 水水~~ /************************************************************************* > Author: x ...
- Unity3D NGUI自适应屏幕分辨率(2014/4/17更新)
原地址:http://blog.csdn.net/asd237241291/article/details/8126619 原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 本文链接地址: ...
- c++ ANSI、UNICODE、UTF8互转
static std::wstring MBytesToWString(const char* lpcszString); static std::string WStringToMBy ...
- MyEclipse server窗口 Could not create the view: An unexpected exception was thrown 错误解决
MyEclipse 打开后有时候莫名的在server窗口里抛出“Could not create the view: An unexpected exception was thrown”错误,解决办 ...
- AAC ADTS AAC LATM 格式分析
http://blog.csdn.net/tx3344/article/details/7414543# 目录(?)[-] ADTS是个啥 ADTS内容及结构 将AAC打包成ADTS格式 1.ADTS ...