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.
InputThe 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).
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].
题解:
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讨论时,每枚举一个交点,为什么只有两种情况
见图(图是盗滴)
所以这有两种情况
#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);
}