You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}