【題目描述】
觀察下面的數字金字塔。寫一個程序查找從最高點到底部任意處結束的路徑,使路徑經過數字的和最大。每一步可以從當前點走到左下方的點也可以到達右下方的點。
在上面的樣例中,從13到8到26到15到24的路徑產生了最大的和86。
【輸入】
第一個行包含R(1≤ R≤1000),表示行的數目。
後面每行爲這個數字金字塔特定行包含的整數。
所有的被供應的整數是非負的且不大於100。
【輸出】
單獨的一行,包含那個可能得到的最大的和。
【輸入樣例】
5
13
11 8
12 7 26
6 14 15 8
12 7 13 24 11
【輸出樣例】
86
// Created on 2020/2/15
/*#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int idata=100000+5;
const int idata2=1000+5;
int n,m;
int cnt,flag;
double minn=INT_MAX,maxx=-1;
int aim[idata2][idata2];
int step[idata2][idata2];
int sum;
bool judge;
int main()
{
int i,j;
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cin>>aim[i][j];
}
}
step[1][1]=aim[1][1];
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
step[i][j]=max(step[i-1][j],step[i-1][j-1])
+aim[i][j];
}
}
for(i=1;i<=n;i++)
if(maxx<step[n][i])
maxx=step[n][i];
cout<<maxx<<endl;
return 0;
}