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++]
  • 相关阅读:
    安装插件 YouCompleteMe 成功却无法自动补全C++的解决办法
    Ubuntu 16.04 使用 vim_plug 安装插件 YouCompleteMe 报错“ycmd server SHUT DOWN”
    POJ #1062 昂贵的聘礼 有限制的最短路 枚举+dijkstra求最短路
    POJ #3259 Wormholes 判负环 SPFA
    POJ #1860 Currency Exchange 最短路径算法 Bellman-ford SPFA 判断负环
    spfa 算法模板 可求带负权边的最短路 判断负环
    POJ #1042 Gone Fishing 贪心
    路人甲
    Perface
    1. Perface
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3267741.html
Copyright © 2011-2022 走看看