F - Monkey Banana Problem

F - Monkey Banana Problem

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure). While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.

                                             

 

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Every case starts with an integer N (1 ≤ N ≤ 100). It denotes that, there will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly i numbers. Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is greater than zero and less than 215.

Output

For each case, print the case number and maximum number of bananas eaten by the monkey.

Sample Input

2

4

7

6 4

2 5 10

9 8 12 2

2 12 7

8 2

10

2

1

2 3

1

Sample Output

Case 1: 63

Case 2: 5

 

//题意第一行是 T 组测试数据,第二行 n 然后是 2*n-1 行的数据

从最上面那里出发,只能选做下走或者右下走,走到最下面那个位置,求路径上最大的和。

//很简单的 dp 注意一些特殊的情况就行

//72ms

 

 1 #include <stdio.h>
 2 #include <string.h>
 3  
 4 int num[210][105];
 5 int dp[210][105];
 6  
 7 int max(int a ,int b)
 8 {return  a>b?a:b;}
 9  
10 int main()
11 {
12     int T,Case=0;
13     int i,j;
14     scanf("%d",&T);
15     while (T--)
16     {
17         int n;
18         scanf("%d",&n);
19         for (i=1;i<=2*n-1;i++)
20         {
21             if (i<=n)
22                 for (j=1;j<=i;j++)
23                     scanf("%d",&num[i][j]);
24             else
25                 for (j=1;j<=2*n-i;j++)
26                     scanf("%d",&num[i][j]);
27         }
28  
29         memset(dp,0,sizeof(dp));
30         for (i=2*n-1;i>=1;i--)
31         {
32             if (i<n)
33             {
34                 for (j=1;j<=i;j++)
35                     dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+num[i][j];   
36             }
37             else
38             {
39                 for (j=1;j<=2*n-i;j++)
40                 {
41                     if (j==1)
42                         dp[i][j]=dp[i+1][j]+num[i+1][j];
43                     if (j!=1&&j==2*n-i)
44                         dp[i][j]=dp[i+1][j-1]+num[i][j];
45  
46                     dp[i][j]=max(dp[i+1][j-1],dp[i+1][j])+num[i][j];
47                 }
48             }
49         }
50         printf("Case %d: ",++Case);
51         printf("%d\n",dp[1][1]);
52     }
53     return 0;
54 }
View Code

 

 

转载于:https://www.cnblogs.com/haoabcd2010/p/5760331.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值