joj2511: Number triangle

 2511: Number triangle


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
1s65536K467233Standard
        5
      3   4
    8   1   2
  5   4   3   6
2   1   7   9   8

A number trangle is composed of N(N<=100) line numbers, the i-th line contains i positive integers(<=100). A chess can walk from the top line of the triangle to the bottom line. Suppose the chess is on the k-th number of one line, then it can only move to the k-th number or the (k+1)-th number of the line below in one step. Find a path from the top to the bottom, which can maximize the sum of the integers on the path.

Input

There are multiple test cases.For each test case, there's an integer N representing the size of the triangle, followed by N lines of positive intergers, the first line has 1 integer, next has two integers... the Nth line has N integers.

Output

The maximum sum of the integers on the path.

Sample Input

3
5
7 0
2 4 3
3
2
6 1
2 3 9

Sample Output

16
12

 

Problem Source: xwbsw

 


This problem is used for contest: 120 


Submit / Problem List / Status / Discuss

 
水DP
倒着推即可
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int a[105][105];
 5 
 6 int main()
 7 {
 8     //freopen("in.txt", "r", stdin);
 9     
10     int n, i, j;
11     
12     while (scanf("%d", &n) == 1)
13     {
14         memset(a, 0, sizeof(a));
15         for (i=1; i<=n; ++i)
16         {
17             for (j=1; j<=i; ++j)
18             {
19                 scanf("%d", &a[i][j]);
20             }
21         }
22         
23         for (i=n; i>1; --i)
24         {
25             for (j=i; j>=1; --j)
26             {
27                 if(a[i][j]+a[i-1][j]>a[i][j+1]+a[i-1][j])
28                     a[i-1][j]=a[i][j]+a[i-1][j];
29                 else
30                     a[i-1][j]=a[i][j+1]+a[i-1][j];
31             }
32         }
33         printf("%d\n", a[1][1]);
34     }
35     
36     return 0;
37 }

 

转载于:https://www.cnblogs.com/RootJie/archive/2012/05/07/2489250.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值