zoukankan      html  css  js  c++  java
  • POJ 3176 Cow Bowling

    Cow Bowling
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12121   Accepted: 7985

    Description

    The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this: 
              7
     
             3   8
     
           8   1   0
     
         2   7   4   4
     
       4   5   2   6   5
    Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame. 
    Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

    Input

    Line 1: A single integer, N 
    Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

    Output

    Line 1: The largest sum achievable using the traversal rules

    Sample Input

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

    Sample Output

    30

    Hint

    Explanation of the sample: 
              7
              *
             3   8
            *
           8   1   0
            *
         2   7   4   4
            *
       4   5   2   6   5
    The highest score is achievable by traversing the cows as shown above.
     
    动态规划最基础最经典的问题
    设走到第i行左数第j个点时能达到的最大值为f(i,j),该点值为num[i][j]
    状态转移方程为f(i,j)=max(f(i-1,j-1),f(i-1,j))+num[i][j](注意边界处要特殊判断)
     
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 int n;
     8 int num[500][500],dp[500][500];
     9 
    10 int f(int x,int y)
    11 {
    12     if(dp[x][y]!=-1)
    13         return dp[x][y];
    14     if(x==1)
    15         return dp[x][y]=num[x][y];
    16     if(y==1)
    17         return dp[x][y]=f(x-1,y)+num[x][y];
    18     if(y==x)
    19         return dp[x][y]=f(x-1,y-1)+num[x][y];
    20     return dp[x][y]=max(f(x-1,y-1),f(x-1,y))+num[x][y];
    21 }
    22 
    23 int main()
    24 {
    25     while(scanf("%d",&n)==1)
    26     {
    27         for(int i=0;i<500;i++)
    28             fill(dp[i],dp[i]+500,-1);
    29         for(int i=1;i<=n;i++)
    30             for(int j=1;j<=i;j++)
    31                 scanf("%d",&num[i][j]);
    32         int ans=0;
    33         for(int j=1;j<=n;j++)
    34             if(f(n,j)>ans)
    35                 ans=dp[n][j];
    36         printf("%d
    ",ans);
    37     }
    38 
    39     return 0;
    40 }
    [C++]
  • 相关阅读:
    欧拉项目第十题;用筛法做,
    欧拉第十题 求2000000以内的素数之和
    求1000数字中13个相乘最大值
    筛法求10000以内的质数
    判断回文且寻找三位数乘三位数里最大的回文
    分解质因数
    输入一个正整数n,将其转换为二进制后输出。要求定义并调用函数dectobin(n),它的功能是输出
    hdu1863最小生成树krus模板&prim模板
    hdu 3870 最小割转化为最短路径2
    UVALive 3661 最小割转化为最短路径
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3267741.html
Copyright © 2011-2022 走看看