这道题要注意的是横坐标取值范围:
x + y = k
=> y = k - x
=> 1 <= y = k - x <= m
=> k - m <= x <= k - 1
for (int k = 2; k <= m + n; ++k)
for (int x1 = max(1, k - m); x1 <= n && x1 < k; ++x1)
for (int x2 = max(1, k - m); x2 <= n && x2 < k; ++x2)
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 55;
int w[N][N];
int f[N << 1][N][N];
int dx[4][2] = { {0, 0}, {0, -1}, {-1, 0}, {-1, -1} };
int main() {
//freopen("in.txt", "r", stdin);
IO;
int m, n;
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> w[i][j];
for (int k = 2; k <= m + n; ++k)
for (int x1 = max(1, k - m); x1 <= n && x1 < k; ++x1)
for (int x2 = max(1, k - m); x2 <= n && x2 < k; ++x2)
for (int i = 0; i < 4; ++i)
if (x1 != x2 || k == 2 || k == m + n) {
int v = w[x1][k - x1] + w[x2][k - x2];
f[k][x1][x2] = max(f[k][x1][x2], f[k - 1][x1 + dx[i][0]][x2 + dx[i][1]] + v);
}
cout << f[m + n][n][n] << ‘\n‘;
return 0;
}
原文:https://www.cnblogs.com/phr2000/p/14635303.html