zoukankan      html  css  js  c++  java
  • F

    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
    ",dp[1][1]);
    52     }
    53     return 0;
    54 }
    View Code

     

     

  • 相关阅读:
    【看完想不会都难的系列教程】- (3) JQuery+JQueryUI+Jsplumb 实现拖拽模块,流程图风格
    数据库~大叔通过脚本生成poco实体
    Git~分支真的很轻
    jenkins~管道Pipeline里使用公用类库
    docker~run起来之后执行多条命令
    jenkins~管道Pipeline的使用,再见jenkinsUI
    通过数组初始化链表的两种方法:指向指针的引用node *&tail和指向指针的指针(二维指针)node **tail
    NYOJ 16 矩形嵌套(动态规划)
    SPOJ 416
    sqlserver,执行生成脚本时“引发类型为“System.OutOfMemoryException”的异常”(已解决)
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/5760331.html
Copyright © 2011-2022 走看看