Poj 3176 cow Bowling

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 3176

 

Idea: dynamic question. DP [I] [J] indicates that the maximum value of the J is selected for the I line;

State transition equation: DP [I] [J] = max (DP [I-1] [J-1] + A [I] [J], DP [I-1] [J] + A [I] [J]) where a [I] [J] represents the value of column J in row I.

 

Code:

#include <iostream>#include <cstring>using namespace std;int dp[351][351];int a[351][351];int main(){    int n;    int res;    while(cin>>n)    {        memset(dp,0,sizeof(dp));        res = 0;                for(int i = 1;i<=n;i++)        {            for(int j=1;j<=i;j++)            {                cin>>a[i][j];                dp[i][j] = max(dp[i-1][j]+a[i][j],dp[i-1][j-1]+a[i][j]);            }        }        for(int i=1;i<=n;i++)        {            res = res<dp[n][i]?dp[n][i]:res;        }        cout<<res<<endl;    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.