思路:
1、动态规划:
2、记忆数组定义和递推式
设dp[i][j] = 从第0行到达该位置所走过的数的和的最大值
i == 0 : dp[0][0] = a[0][0]
i>0:
j : dp[i][j] = dp[i-1][j]+a[i][j]
j == i : dp[i][j] = dp[i-1][j-1]+a[i][j];
j>0&&j<i : dp[i][j] = max(dp[i-1][j-1]+a[i][j],dp[i-1][j]+a[i][j]);
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include<iostream>
#include<map>
#include<set>
#include<math.h>
using namespace std;
#define MAX_A 10000
//动态规划
//时间复杂化度为O(n*n)
int main(){
//输入
int n;
scanf("%d",&n);
int dp[n][n];
int a[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
scanf("%d",&a[i][j]);
dp[i][j] = 0;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
if(i==0){
dp[i][j] = a[i][j];
}else if(j==0){
dp[i][j] = dp[i-1][j]+a[i][j];
}else if(j==i){
dp[i][j] = dp[i-1][j-1]+a[i][j];
}else{
dp[i][j] = max(dp[i-1][j-1]+a[i][j],dp[i-1][j]+a[i][j]);
}
}
}
int maxx = 0;
for(int i=0;i<n;i++){
maxx = max(maxx,dp[n-1][i]);
}
printf("%d\n",maxx);
return 0;
}