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++]
  • 相关阅读:
    slf4j的使用
    hashMap
    HBase
    HBase应用快速开发
    初学MongoDB 遇到提示由于目标计算机积极拒绝,无法连接
    Flask学习中运行“helloworld”出现UnicodeDecodeError: 'utf-8' codec can't decode问题
    doGet或doPost方法没有调用的一个原因
    markdown测试
    tomcat集成到IDEA与部署项目
    tomcat部署项目的方式
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3267741.html
Copyright © 2011-2022 走看看