0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-10 14:38:47 +00:00
OI-codes/Luogu/P7074/P7074.cpp

35 lines
845 B
C++
Raw Normal View History

2020-11-15 07:12:33 +00:00
#include <bits/stdc++.h>
using namespace std;
2021-11-19 09:01:13 +00:00
int n, m;
2020-11-15 07:12:33 +00:00
long long w[1005][1005], f[1005][1005][2];
long long dfs(int x, int y, int from) {
if (x < 1 || x > n || y < 1 || y > m) {
return -0x3f3f3f3f;
}
if (f[x][y][from] != -0x3f3f3f3f) {
return f[x][y][from];
}
if (from == 0) {
f[x][y][from] = max({dfs(x + 1, y, 0), dfs(x, y - 1, 0), dfs(x, y - 1, 1)}) + w[x][y];
2021-11-19 09:01:13 +00:00
} else {
2020-11-15 07:12:33 +00:00
f[x][y][from] = max({dfs(x - 1, y, 1), dfs(x, y - 1, 0), dfs(x, y - 1, 1)}) + w[x][y];
}
return f[x][y][from];
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> w[i][j];
f[i][j][0] = f[i][j][1] = -0x3f3f3f3f;
}
}
f[1][1][0] = f[1][1][1] = w[1][1];
cout << dfs(n, m, 1) << endl;
return 0;
}