Thinking
Seeking a maximum value, the DP is in general. The subject has shown signs of such cost flow, and construction side is obvious, but too much trouble, there is no write ...... DP program this problem is not difficult, since it is back and forth, then we can put a man split into two people, ah, so for "first person" enumeration
Finally theoretically output
Code
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
int nextInt()
{
int num = 0;
char c;
bool flag = false;
while ((c = std::getchar()) == ' ' || c == '\r' || c == '\t' || c == '\n');
if (c == '-')
flag = true;
else
num = c - 48;
while (std::isdigit(c = std::getchar()))
num = num * 10 + c - 48;
return (flag ? -1 : 1) * num;
}
int max4(const int a, const int b, const int c, const int d)
{
int x = std::max(a, b);
if (c > x)
x = c;
if (d > x)
x = d;
return x;
}
const size_t _Siz = 51u;
int f[_Siz][_Siz][_Siz][_Siz] = { 0 };
int a[_Siz][_Siz] = { 0 };
int n, m;
int main(int argc, char **argv)
{
n = nextInt();
m = nextInt();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
a[i][j] = nextInt();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int k = 1; k <= n; k++)
for (int l = j + 1; l <= m; l++)
f[i][j][k][l] = max4(f[i][j - 1][k - 1][l], f[i - 1][j][k][l - 1], f[i][j - 1][k][l - 1], f[i - 1][j][k - 1][l]) + a[i][j] + a[k][l];
std::cout << f[n][m - 1][n - 1][m] << std::endl;
#ifdef __EDWARD_EDIT
std::cin.get();
std::cin.get();
#endif
return 0;
}