HDU 2084 Tower---Getting Started DP

Source: Internet
Author: User

HDU 2084 Number Tower

Recursive from bottom to top, state transfer equation Dp[i][j] = max (Dp[i+1][j], dp[i+1][j+1]) + a[i][j];

/*HDU 2084 Tower---Getting Started DP*/#include<cstdio>Const intN = the;intDp[n][n];intMAX (intAintb) {    returna > B?a:b;}intMain () {#ifdef _local freopen ("D:\\input.txt","R", stdin); #endif    intT; scanf ("%d", &t);  while(t--){        intN; scanf ("%d", &N);  for(inti =1; I <= N; ++i) {             for(intj =1; J <= I; ++j) {scanf ("%d", &Dp[i][j]); }        }        ////dp[i][j] Record the maximum path from the bottom up to the i,j, the final result and the dp[1][1] is the longest path        //for (int i = 1; I <= n; ++i) {//    //The DP of the last line is initialized to the value of a[i][j] itself; //Dp[n][i] = a[n][i]; //}        //state transition equation: dp[i][j] = max (D[i+1][j] + dp[i+1][j+1]) + a[i][j]//That is, the maximum number of two adjacency points to the next line plus the value of that point is the maximum value for that point.         for(inti = n-1; i >0; --i) {            //line I has the number of I, from the bottom to go up,             for(intj =1; J <= I; ++j) {Dp[i][j]= MAX (Dp[i +1][J], Dp[i +1][j +1]) +Dp[i][j]; //use Dp[i][j directly] to omit the value of an array//Dp[i][j] = MAX (Dp[i + 1][j], dp[i + 1][j + 1]) + a[i][j];            }        }        //recursive from the bottom to eliminate the last cycle to find the maximum value and go directly to Dp[1][1]printf"%d\n", dp[1][1]); }    return 0;}
View Code

HDU 2084 Tower---Getting Started DP

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.