15hdu_oj--Number Tower

Source: Internet
Author: User

/*
* Copyright (c) 2014, Yantai University School of Computer Science
* All rights reserved.
* File name: test.cpp
* Author: Xiaokai
* Completion Date: June 16, 2015
* Version Number: v1.0
*
* Description of the problem:
* Input Description:
* Program output:

*/

problem Description when it comes to DP algorithms, a classic example is the tower problem, which is described in this way:

As shown in the tower, it is required to go from the top floor to the bottom, if each step can only go to adjacent nodes, then the number of nodes through the sum of the maximum is how much.

Already told you, this is a DP problem, can you AC? input data first includes an integer c, which indicates the number of test instances, the first line of each test instance is an integer N (1 <= N <= 100), which represents the height of the tower, followed by N-rows of numbers to denote the tower, where line I has an integer, And all integers are within the interval [0,99].
output for each test instance, the outputs may be the largest and one row per instance output.
Sample Input

1
5
7
3 8
8 1 0 
2 7 4 4
4 5 2 6 5
Sample Output
30

Analytical:

Record the number of the bottom of the tower first, then start looking up, select the largest number in the upper level and add it until the top of the tower, so that you can find the path of the number on the path and the largest one.

Code:


#include <stdio.h>
#include <string.h>
int max (int a,int b)
{
    return (a>b) a:b;
}
int main ()
{
    int cases,n,num[101][101],f[10001];
    int i,j;
    scanf ("%d", &cases);
    while (cases--)
    {
        memset (f,0,sizeof (f));
        scanf ("%d", &n);
        for (i=1;i<=n;i++)
            for (j=1;j<=i;j++)
                scanf ("%d", &num[i][j]);
        for (i=1;i<=n;i++)
            f[i]=num[n][i];
        for (i=n-1;i>=1;i--) for
            (j=1;j<=i;j++)
            F[j]=max (F[j]+num[i][j],f[j+1]+num[i][j]);
        printf ("%d\n", f[1]);
    }
    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.