Working out(DP 四个角递推)

B. Working out
time limit per test
2 seconds
memory limit per test
256 megabytes

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.

Examples
Input
Copy
3 3
100 100 100
100 1 100
100 100 100
Output
Copy
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].


题意

给点n*m矩阵表示地图,地图中每个点有一个权值,有2个人A、B,A从(1,1)出发,前往(n,m),B从(n,1)出发,前往(1,m),2个人每经过一个点就能获得该点的权值(但每个点的权值只能被算1次),且2人至少相遇一次,且不能获得相遇的那个点的权值,但2人的速度可以不同,问2人能获得的最大权值和。此外,A只能往下或往右走,B只能往上或往右走。


扫描二维码关注公众号,回复: 2734766 查看本文章

思路

预处理出每个点到4个角所能得到的最大权值。由题意可以看出,相遇所能得到的价值最小,所以2人相遇次数越少越好,即1次为最优。因此,枚举每个点作为相遇点,求出所能得到的最大权值和。


此外,需要注意的是每个点的权值只能被计算一次,因此一个点A走了,B就尽可能不要去走。因此,我们需要枚举A、B进来和出去的方式。通过画图可得,只有2种。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;

const int MAX = 1000+100;
const ll INF = 1e15;

int n,m;
ll mp[MAX][MAX];
ll dp[5][MAX][MAX];

int main()
{
    memset(dp,0,sizeof(dp));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%lld",&mp[i][j]);

    //预处理每个点到4个角的距离
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            dp[1][i][j]=max(dp[1][i-1][j],dp[1][i][j-1])+mp[i][j];
        for(int j=m;j>=1;j--)
            dp[2][i][j]=max(dp[2][i-1][j],dp[2][i][j+1])+mp[i][j];
    }

    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;j++)
            dp[3][i][j]=max(dp[3][i+1][j],dp[3][i][j-1])+mp[i][j];
        for(int j=m;j>=1;j--)
            dp[4][i][j]=max(dp[4][i+1][j],dp[4][i][j+1])+mp[i][j];
    }
    
    //把边界值设为负无穷
    for(int i=0;i<=n+1;i++)
    {
        dp[1][i][0]=dp[2][i][0]=dp[3][i][0]=dp[4][i][0]=-INF;
        dp[1][i][m+1]=dp[2][i][m+1]=dp[3][i][m+1]=dp[4][i][m+1]=-INF;
    }
    for(int i=0;i<=m+1;i++)
    {
        dp[1][0][i]=dp[2][0][i]=dp[3][0][i]=dp[4][0][i]=-INF;
        dp[1][n+1][i]=dp[2][n+1][i]=dp[3][n+1][i]=dp[4][n+1][i]=-INF;
    }

    //枚举相遇点
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            //枚举2种进出方式
            ll tmp=dp[1][i-1][j]+dp[4][i+1][j]+dp[3][i][j-1]+dp[2][i][j+1];
            ans=max(ans,tmp);
            tmp=dp[1][i][j-1]+dp[4][i][j+1]+dp[3][i+1][j]+dp[2][i-1][j];
            ans=max(ans,tmp);
        }
    }

    printf("%lld\n",ans);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/luyehao1/article/details/81047240
今日推荐