ime Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45620   Accepted: 27612

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<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int N=;
int mp[N][N];
int dp[N][N];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&mp[i][j]); }
dp[i][i]=dp[i-][i-]+mp[i][i];
} for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
if(i==) dp[i][j]=mp[i][j];
else if(j==) dp[i][j]=dp[i-][j]+mp[i][j];
else if(j==i) dp[i][j]=dp[i-][j-]+mp[i][j];
else dp[i][j]=max(dp[i-][j-]+mp[i][j],dp[i-][j]+mp[i][j]);
}
}
int ans=;
for(int i=;i<=n;i++)
{
if(ans<dp[n][i])
ans=dp[n][i];
}
cout<<ans<<endl;
}
return ;
}

The Triangle_DP的更多相关文章

随机推荐

  1. Uva-------(11462) Age Sort(计数排序)

    B Age Sort Input: Standard Input Output: Standard Output   You are given the ages (in years) of all ...

  2. 初学java之触发响应事件

    import java.awt.*; import javax.swing.*; import java.awt.event.*; class WindowActionEvent extends JF ...

  3. poj2676 Sudoku(DFS)

    做了很久还是参考了别人的答案orz,其实也不难啊.我要开始学一下怎么写搜索了... 题目链接:poj2676 Sudoku 题解:暴力搜索,DFS每个空白格子所放数字. #include<cst ...

  4. #ifdef __cplusplus extern "C"

    #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #endif首先,__cplusplus是cp ...

  5. <<薪资至少10K的一道题,你能拿下吗>>练习

    偶尔要写写算法,是我平时用来保持感觉的常用的方法.今天看到园子里一面试题,看了一下感觉也能实现,不过过程确实艰的,自认为自己对算法的感觉还不错.不过这题确实我也用了差不多一下午的时间,基本上把工作时间 ...

  6. 三级菜单---zhufeng

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  7. 我的Github注册使用之旅

    [个人介绍] 我是来自网络工程143班的姜金金,学号是1413042066.我没什么大的爱好,闲时喜欢在有阳光的午后喝喝小茶,捧一本书慢慢品茗:也喜欢散散步,欣赏细碎事物的美好,驻足沿路美丽的风景.说 ...

  8. 《Play for Java》学习笔记(七)数据类型解析——Body parser

    一.什么是body parser? body parser(不知道具体如何翻译,~~~~(>_<)~~~~ )指一个HTTP请求 (如POST和PUT操作)所包含的文本内容(body),这 ...

  9. 8种NOsql

    虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只是时间问题:被迫使用关系数据库,但最终发现不能适应需求的情况不胜枚举. 但是NoSQL数据库之间的不同,远超过两 SQ ...

  10. css改变图片的颜色

    参考大神张鑫旭:http://www.zhangxinxu.com/wordpress/2016/06/png-icon-change-color-by-css/ 主要知识点:border-right ...