Files
python/TangDou/CSP-J/J2_2020/4.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

38 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1010;
const LL INF = 1e18;
LL f[N][N][3];
LL a[N][N];
int n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
f[i][j][0] = f[i][j][1] = f[i][j][2] = -INF;
}
// 初始化
f[1][1][0] = f[1][1][1] = a[1][1];
for (int i = 2; i <= n; i++) f[i][1][1] = f[i - 1][1][1] + a[i][1];
for (int j = 2; j <= m; j++) {
for (int i = 1; i <= n; i++)
f[i][j][0] = max({f[i][j - 1][1], f[i][j - 1][0], f[i][j - 1][2]}) + a[i][j];
for (int i = 2; i <= n; i++)
f[i][j][1] = max(f[i - 1][j][0], f[i - 1][j][1]) + a[i][j];
for (int i = n - 1; i >= 1; i--)
f[i][j][2] = max(f[i + 1][j][0], f[i + 1][j][2]) + a[i][j];
}
cout << max(f[n][m][0], f[n][m][1]);
// 1e3 * 1e3 * 1e4=1e10=10000000000>2147483647 因为 1e10是11位数,INT_MAX是10位数,肯定会超,要开long long
return 0;
}