The Triangle(数塔)

来源:互联网 发布:武汉淘宝摄影 编辑:程序博客网 时间:2024/06/05 10:46
The Triangle
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu


Description
         7
      3   8
    8   1   0
  2   7   4   4
4   5   2   6   5

(Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right. 

Input

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

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

题目大意:

    给你一个n层的三角形,从顶点开始,每次只能加左下或右下的数,请你找到此三角形从上到下相加之和最大为多少。

<span style="font-family: Arial, Helvetica, sans-serif;">//AC..【二维数组做法】.动规(从上向下推)...</span>
#include<cstdio>#include<algorithm>using namespace std;int main(){    int n,a[110][110];    while (scanf("%d",&n)!=EOF)    {        int dp[110][110]={0};        for (int i=0;i<n;i++)            for (int j=0;j<i+1;j++)                scanf("%d",&a[i][j]);        for (int i=0;i<n;i++)        {            for (int j=0;j<i+1;j++)            {                if (i==0)                    dp[i][j]=a[i][j];                else if (j==0)                    dp[i][j]=a[i][j]+dp[i-1][j];                else if (i==j)                    dp[i][j]=a[i][j]+dp[i-1][j-1];                else                    dp[i][j]=a[i][j]+max(dp[i-1][j],dp[i-1][j-1]);            }        }//        for (int i=0;i<n;i++)//        {//            for (int j=0;j<i+1;j++)//            {//                printf("%-d ",dp[i][j]);//            }//            printf("\n");//        }        int maxsum=0;        for (int i=0;i<n;i++)        {            if (maxsum<dp[n-1][i])                maxsum=dp[n-1][i];        }        printf("%d\n",maxsum);    }    return 0;}
【图解】

原图:
i\j   0     1      2     3    4

0    7

1    3     8

2    8     1     0

3    2     7     4     4

4    4     5     2     6     5

结果:
i\j   0     1     2     3     4

0    7

1   10  15

2   18  16   15

3   20   25   20  19

4   24   30   27   26  24

//AC...【二维数组做法】动规(从下向上推)...#include<cstdio>#include<algorithm>using namespace std;int main(){    int n,a[110][110];    while (scanf("%d",&n)!=EOF)    {        for (int i=0;i<n;i++)            for (int j=0;j<i+1;j++)                scanf("%d",&a[i][j]);        int dp[110][110]={0};        for (int i=n-1;i>=0;i--)        {            for (int j=i;j>=0;j--)            {                if (i==n-1)                    dp[i][j]=a[i][j];                else                    dp[i][j]=a[i][j]+max(dp[i+1][j],dp[i+1][j+1]);            }        }//        for (int i=0;i<n;i++)//        {//            for (int j=0;j<i+1;j++)//            {//                printf("%-d ",dp[i][j]);//            }//            printf("\n");//        }        printf("%d\n",dp[0][0]);    }    return 0;}
【图解】

原图:
i\j   0     1      2     3    4

0    7

1    3     8

2    8     1     0

3    2     7     4     4

4    4     5     2     6     5

结果:
i\j     0       1      2      3      4

0     30

1     23     21

2     20     13   10

3     7       12   10    10

4     4        5     2       6      5

嗯,偷笑其实这道题还不算完,虽然这道题的时间复杂度已经没法优化了,不过空间复杂度却还可以优化,可以用一个一维数组的dp替换二维数组的dp。

//【一维数组做法】:优化了空间复杂度。#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[110];int a[110][110],n;int main(){    while (scanf("%d",&n)!=EOF)    {        for (int i=0;i<n;i++)            for (int j=0;j<i+1;j++)                scanf("%d",&a[i][j]);        memset(dp,0,sizeof(dp));        for (int i=n-1;i>=0;i--)        {            //j的值要从小到大,因为后面要用到dp[j+1]的值,不能再使用前覆盖掉            for (int j=0;j<=i;j++)              {                if (i==n-1)                    dp[j]=a[i][j];                else                    dp[j]=a[i][j]+max(dp[j+1],dp[j]);//                printf("%d ",dp[j]);            }//            printf("\n");        }        printf("%d\n",dp[0]);    }    return 0;}



0 0