AcWing 2770. 方格取数
原题链接
中等
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1010;
int n, m;
int w[N][N];
LL f[N][N];
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> w[i][j];
memset(f, -0x3f, sizeof f);
f[1][0] = 0;
for(int j = 1; j <= m; j++){
LL s = -1e18;
for(int i = 1; i <= n; i++){
s = max(s, f[i][j - 1]) + w[i][j];
f[i][j] = max(s, f[i][j]);
}
s = -1e18;
for(int i = n; i >= 0; i--){
s = max(s, f[i][j - 1]) + w[i][j];
f[i][j] = max(s, f[i][j]);
}
}
cout << f[n][m] << endl;
return 0;
}