刚学动态规划的时候都做过入门题 — 数字金字塔
倒着推
代码如下
#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;
#define in = read();
typedef long long ll;
typedef unsigned int ui;
const ll size = 200 + 10;
int m , n;
int map[size][size];
int ans;
int f[size][size];
inline ll read(){
ll num = 0 , f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
num = num*10 + ch - '0';
ch = getchar();
}
return num*f;
}
int main(){
n in; m in;
for(register int i=1;i<=n;i++)
for(register int j=1;j<=m;j++)
map[i][j] in;
for(register int i=1;i<=m;i++)
f[1][i] = map[1][i];
for(register int i=2;i<=n;i++)
for(register int j=1;j<=m;j++)
f[i][j] = max(f[i - 1][j - 1] , max(f[i - 1][j] , f[i - 1][j + 1])) + map[i][j];
ans = max(f[n][m>>1] , max(f[n][(m>>1) + 1] , f[n][(m>>1) + 2]));
printf("%d" , ans);
}
//COYG