天天看點

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 mcolumns. 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] ora[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 eithera[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 elementa[i][j] (0 ≤ a[i][j] ≤ 105).

Output

The output contains a single number — the maximum total gain possible.

Example 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].

題解:

Orz,看到這題的時候已經懵逼了,然後搜題解,問同學,一氣呵成……

(每次遇到不會的感覺好難的題,一搜題解,大佬們都說是水題……憂傷,嘤嘤嘤qwq)

分析:

設4個數組從4個角分别搜尋dp1到dp4

dp1[i][j]表示的是從(1,1)到(i,j)的最大值

我看别人題解是一共有3個疑問:

1.為什麼從4個角搜尋:

假設a,b相遇點為點A(i,j)那麼題中所求的最大值為,左上到A,A到右下,左下到A,A到右上

dp1[i][j]僅僅是(1,1)到A點的最大值,也需要求dp4[i][j](右下到A的最大距離),相應的也要求dp2[i][j],dp3[i][j]

2.為什麼代碼結尾的兩個for循環不考慮4個邊界:

由題知,a,b的所走的路徑畫出來隻能有一個交點,也就是交點1個,且a不能走b走過的路,b也不能走a走過的路

若在邊界相遇:eg:

在(3,1),則a的上一個位置時(2,1),b是(4,1),a的下一個位置是(3,2)或(4,1),b的下一位置是(3,2)或(2,1)

不符合是以不考慮邊界。

3.代碼後部ans讨論時,每枚舉一個交點,為什麼隻有兩種情況

見圖(圖是盜滴)

Working out(詳解)

是以這有兩種情況

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int dp1[1003][1003],dp2[1003][1003],dp3[1003][1003],dp4[1003][1003];
int a[1003][1003];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    memset(dp1,0,sizeof(dp1));
    memset(dp2,0,sizeof(dp2));
    memset(dp3,0,sizeof(dp3));
    memset(dp4,0,sizeof(dp4));

    for(int i=1;i<=n;i++){//左上
        for(int j=1;j<=m;j++){
            dp1[i][j]=max(dp1[i][j-1],dp1[i-1][j])+a[i][j];
        }
    }

    for(int i=n;i>=1;i--){//左下
        for(int j=1;j<=m;j++){
            dp2[i][j]=max(dp2[i+1][j],dp2[i][j-1])+a[i][j];
        }
    }
    for(int i=1;i<=n;i++){//右上
        for(int j=m;j>=1;j--){
            dp3[i][j]=max(dp3[i][j+1],dp3[i-1][j])+a[i][j];
        }
    }
    for(int i=n;i>=1;i--){//右下
        for(int j=m;j>=1;j--){
            dp4[i][j]=max(dp4[i+1][j],dp4[i][j+1])+a[i][j];
        }
    }


    int ans=0;
    for(int i=2;i<n;i++){
        for(int j=2;j<m;j++){
            ans=max(ans,dp1[i][j-1]+dp4[i][j+1]+dp3[i-1][j]+dp2[i+1][j]);
            ans=max(ans,dp1[i-1][j]+dp4[i+1][j]+dp3[i][j+1]+dp2[i][j-1]);
        }
    }
    printf("%d\n",ans);
}