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.
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).
The output contains a single number — the maximum total gain possible.
3 3 100 100 100 100 1 100 100 100 100
800
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].
这道题需注意所走路线不能重合;
要去枚举所有相遇的点;
需要开四个数组表示在i,j相遇的最大值,分别是从
a[1][1]-->a[n,m];
b[n][1]-->b[1][
c[1][m]-->c[n][
d[n][m]-->d[1][1];
eg.
假设在(2,2)处相遇
case 1:
a (in) | |||
b (in) | meet | b (out) | |
a (out) | |||
case 2:
b (out) | |||
a (in) | meet | a (out) | |
b (in) | |||
比较这两种情况的最大值并记录下来.
输出所有相遇点中最大的即可.
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1005][1005];
int b[1005][1005];
int c[1005][1005];
int d[1005][1005];
int e[1005][1005];
int f[1000005];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=0;i<=n+1;i++) a[i][0]=0;
for (int j=0;j<=m+1;j++) a[0][j]=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
b[i][j]=max(b[i-1][j]+a[i][j],b[i][j-1]+a[i][j]);
}
for (int i=n;i>=1;i--)
for (int j=1;j<=m;j++)
{
c[i][j]=max(c[i+1][j]+a[i][j],c[i][j-1]+a[i][j]);
}
for (int i=1;i<=n;i++)
for (int j=m;j>=1;j--)
{
d[i][j]=max(d[i][j+1]+a[i][j],d[i-1][j]+a[i][j]);
}
for (int i=n;i>=1;i--)
for (int j=m;j>=1;j--)
{
e[i][j]=max(e[i+1][j]+a[i][j],e[i][j+1]+a[i][j]);
}
int coutt=0;
for (int i=2;i<=n-1;i++)
for (int j=2;j<=m-1;j++)
{
if ((b[i-1][j]+e[i+1][j]+c[i][j-1]+d[i][j+1])>(b[i][j-1]+e[i][j+1]+c[i+1][j]+d[i-1][j])) f[coutt]=b[i-1][j]+e[i+1][j]+c[i][j-1]+d[i][j+1];
else f[coutt]=b[i][j-1]+e[i][j+1]+c[i+1][j]+d[i-1][j];
coutt++;
}
int ans=-1;
for (int i=0;i<coutt;i++)
{
if (f[i]>ans) ans=f[i];
}
printf("%d\n",ans);
}