当前位置:网站首页>7-7 digital triangle

7-7 digital triangle

2022-06-24 23:31:00 White -

7-7 Digital triangle

Look at the digital pyramid below . Write a program to find the path from the top to the bottom , Make the path go through the number and maximum . Each step can go from the current point to the lower left point or to the lower right point .
 Insert picture description here
In the example above , from 13 To 8 To 26 To 15 To 24 The path of produces the largest sum 86.

Input format :
The first line contains R(1≤ R≤1000), Represents the number of rows .

The number of integers contained in a specific line of the pyramid of numbers in each subsequent line .

All supplied integers are nonnegative and not greater than 100.

Output format :
A single line , Include the largest sum that can be obtained .

sample input :

5
13
11 8
12 7 26
6 14 15 8
12 7 13 24 11

sample output :

86

Code :

#include <stdio.h>
#include <stdlib.h>
int n;
int a[1010][1010];
int vis[1010][1010];
int findmax(int a,int b)
{
    
    return a>=b?a:b;
}
int fid(int x,int y)
{
    
    if(x>n||y>n)
        return 0;
    if(vis[x][y]!=0)
        return vis[x][y];
    int l=fid(x+1,y)+a[x][y];
    int r=fid(x+1,y+1)+a[x][y];
    return vis[x][y]=findmax(l,r);
}
int main()
{
    
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=i;j++)
            scanf("%d",&a[i][j]);
    printf("%d",fid(1,1));
    return 0;
}

202206222103 3、 ... and

原网站

版权声明
本文为[White -]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241820371057.html