codevs 1220 数字三角形

1220 数字三角形

 

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
 
题目描述 Description

如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或得向右走,一直走到底层,要求找出一条路径,使路径上的值最大。

输入描述 Input Description

第一行是数塔层数N(1<=N<=100)。

第二行起,按数塔图形,有一个或多个的整数,表示该层节点的值,共有N行。

输出描述 Output Description

输出最大值。

样例输入 Sample Input

5

13

11 8

12 7 26

6 14 15 8

12 7 13 24 11

样例输出 Sample Output

86

数据范围及提示 Data Size & Hint
数字三角形
连接节点的下两个节点取 最大值+自身节点数
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int N=500;
 6 int dp[N][N];
 7 int sd[N][N];
 8 int main()
 9 {
10     int n;
11     cin>>n;
12     for(int i=1;i<=n;i++)
13     {
14         for(int j=1;j<=i;j++)
15         {
16             scanf("%d",&dp[i][j]);
17         }
18     }
19     for(int i=n-1;i>=1;i--)
20     {
21         for(int j=1;j<=i;j++)
22         dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+dp[i][j];
23     }
24     cout<<dp[1][1];
25     return 0;
26 }

 

posted @ 2017-05-06 15:20  zzzzx  阅读(163)  评论(0编辑  收藏  举报