Working out
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column.
Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j].
There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.
If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.
Input
The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).
Output
The output contains a single number — the maximum total gain possible.
Input
3 3 100 100 100 100 1 100 100 100 100
Output
800
Note
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].
思路:
两个人,一个从左上角出发到右下角,一个从左下角出发到右上角,找到两人的健身量之和最大,要求两人路径必须只有一个交点。先建立4个二维dp数组,来算出四个角到各个位置的最大健身量。像dp1, 就用动态规划方法,算出从左上角开始到每个格的最大健身量。同理算出其他三个。这样我们可以认为,对于每个位置,dp1中存的是第一个人从起点(左上角)到这个位置的最大健身量,dp2中存的是这个人从这个位置到终点(右下角)的最大健身量,类似的,dp4中存的是第二个人从起点(左下角)到这个位置的健身量,dp3中存的是这个人从这个位置到终点(右上角)的最大健身量。
需要说明的是:在用动态规划算最大健身量时,从a点到各点的最大健身量就是各点到a点的最最大健身量。
之后,我们枚举每个位置,因为交点有且仅有1个,所以每个位置有两种情况(画×的位置是两个路径的交点):
所以,对于每个被枚举的格子,算出四个格对应的dp之和,找到这个结果最大的,就是答案。
AC代码:
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 1005
long long dp1[MAXN][MAXN], dp2[MAXN][MAXN], dp3[MAXN][MAXN], dp4[MAXN][MAXN];
int a[MAXN][MAXN], n, m;
void init()
{
int i, j;
dp1[1][1] = a[1][1];
dp2[n][m] = a[n][m];
dp3[1][m] = a[1][m];
dp4[n][1] = a[n][1];
for(i = 2; i <= n; i++)
{
dp1[i][1] = a[i][1] + dp1[i-1][1];
dp2[n-i+1][m] = a[n-i+1][m] + dp2[n-i+2][m];
dp3[i][m] = a[i][m] + dp3[i-1][m];
dp4[n-i+1][1] = a[n-i+1][1] + dp4[n-i+2][1];
}
for(i = 2; i <= m; i++)
{
dp1[1][i] = a[1][i] + dp1[1][i-1];
dp2[n][m-i+1] = a[n][m-i+1] + dp2[n][m-i+2];
dp3[1][m-i+1] = a[1][m-i+1] + dp3[1][m-i+2];
dp4[n][i] = a[n][i] + dp4[n][i-1];
}
for(i = 2; i <= n; i++)
{
for(j = 2; j <= m; j++)
{
dp1[i][j] = a[i][j] + max(dp1[i-1][j], dp1[i][j-1]);
}
}
for(i = n-1; i >= 1; i--)
{
for(j = m-1; j >= 1; j--)
{
dp2[i][j] = a[i][j] + max(dp2[i+1][j], dp2[i][j+1]);
}
}
for(i = 2; i <= n; i++)
{
for(j = m-1; j >= 1; j--)
{
dp3[i][j] = a[i][j] + max(dp3[i-1][j], dp3[i][j+1]);
}
}
for(i = n-1; i >= 1; i--)
{
for(j = 2; j <= m; j++)
{
dp4[i][j] = a[i][j] + max(dp4[i+1][j], dp4[i][j-1]);
}
}
}
int main()
{
scanf("%d%d", &n, &m);
int i, j;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
scanf("%d", &a[i][j]);
}
}
init();
long long ans = 0;
for(i = 2; i <= n-1; i++)
{
for(j = 2; j <= m-1; j++)
{
ans = max(ans, dp1[i-1][j] + dp2[i+1][j] + dp4[i][j-1] + dp3[i][j+1]);
ans = max(ans, dp1[i][j-1] + dp2[i][j+1] + dp4[i+1][j] + dp3[i-1][j]);
}
}
printf("%lld\n", ans);
return 0;
}