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

Your 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.

Output

Your 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

对于这题若按照常规可能做不出来;但是用递推的思想就可以计算出来啦

代码:

 #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 之解题报告的更多相关文章

  1. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  2. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  3. poj 1163 The Triangle &amp;poj 3176 Cow Bowling (dp)

    id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...

  4. 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 ...

  5. OpenJudge/Poj 1163 The Triangle

    1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...

  6. POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49955   Accepted: 30177 De ...

  7. POJ 1163 The Triangle(经典问题教你彻底理解动归思想)

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38195   Accepted: 22946 De ...

  8. POJ 1163 The Triangle 简单DP

    看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...

  9. 【LeetCode】611. Valid Triangle Number 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...

随机推荐

  1. linux驱动系列之tftp(转)

    转自网页:http://blog.csdn.net/xingyu19871124/article/details/7315893 最近在将做的嵌入式项目移植到ARM开发板上,宿主机用的ubuntu11 ...

  2. CoInitialize()、CoInitializeEx()和AfxOleInit()区别联系

    CoInitialize()和AfxOleInit() 都是初始化COM库,不同之处在与: OLE是建立在COM之上的技术,层次比COM要高.AfxOleInit()调用的是OleInitialize ...

  3. 使用nodejs搭建服务器显示HTML页面

    首先安装express 在命令行输入:npm install express -g 安装完成后可以查看安装情况:npm ls -g 然后创建server.js文件 var express = requ ...

  4. 1058: [ZJOI2007]报表统计 - BZOJ

    Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个非 ...

  5. uva 558 Bellman_Ford

    Bellman_Ford算法   求图中是否存在负权值的回路   若图中不存在   则最短路最多经过n-1个结点   若经过超过n-1个节点 则存在负权值的回路  此图永远无法找到最短路  每条边最多 ...

  6. FireFly 服务端 Unity3D黑暗世界 客户端 问题

    启动服务端成功截图: 连接成功截图: 测试服务端是否启动成功: 在Web输入:http://localhost:11009/  按回车 (查看cmd启动的服务端 是否多出如下显示) 服务端启动成功.P ...

  7. python中unicode、utf8、gbk等编码问题

    转自:http://luchanghong.com/python/2012/07/06/python-encoding-with-unicode-and-gbk-and-utf8.html 概要:编码 ...

  8. JavaWeb学习总结(四十九)——简单模拟Sping MVC

    在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...

  9. QT插件开发方式(作者有RemOjbects文档翻译(48)篇)

    创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下: 1.接口文件源码 #ifndef PLUGININTERFACE_ ...

  10. POJ2993——Emag eht htiw Em Pleh(字符串处理+排序)

    Emag eht htiw Em Pleh DescriptionThis problem is a reverse case of the problem 2996. You are given t ...