-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0275.h
More file actions
52 lines (45 loc) · 1.4 KB
/
Problem0275.h
File metadata and controls
52 lines (45 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Created by Fengwei Zhang on 2021/7/13.
//
#ifndef ACWINGSOLUTION_PROBLEM0275_H
#define ACWINGSOLUTION_PROBLEM0275_H
#include <iostream>
#include <cstring>
using namespace std;
class Problem0275 {
// https://www.acwing.com/solution/content/3954/
private:
static const int N = 50;
int graph[N + 1][N + 1];
int maxValue(const int m, const int n) {
int dp[m + 1][m + 1];
memset(dp, 0, sizeof dp);
for (int s = 2; s <= n + m; ++s) {
for (auto x1 = min(m, s - 1); x1 >= 1; --x1) {
for (auto x2 = min(m, s - 1); x2 >= 1; --x2) {
int v = graph[x1][s - x1];
if (x1 != x2) {
v += graph[x2][s - x2];
}
dp[x1][x2] = max(dp[x1][x2], dp[x1][x2] + v);
dp[x1][x2] = max(dp[x1][x2], dp[x1 - 1][x2] + v);
dp[x1][x2] = max(dp[x1][x2], dp[x1][x2 - 1] + v);
dp[x1][x2] = max(dp[x1][x2], dp[x1 - 1][x2 - 1] + v);
}
}
}
return dp[m][m];
}
int main() {
int m, n;
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
scanf("%d", &graph[i][j]);
}
}
printf("%d\n", maxValue(m, n));
return 0;
}
};
#endif //ACWINGSOLUTION_PROBLEM0275_H