1. 程式人生 > >Working out(dp)給定一個數組求最值

Working out(dp)給定一個數組求最值

題目的大意是給定一個數組,有兩個人在走,其中一個從(1,1)  ->(n,m)另一個是從(n, 1)- > (1,m)在這期間他們會相遇一次,相遇的時候不加陣列值,求兩個人走到終點的時候最大和是多少。題目如下:

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 nlines 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

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

要想使得兩個人相遇就必須一個從上到下一個從左到右。

對於他們相遇的點在到達之前有四個方向。

所以從四個方向考慮:

dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + a[i][j];

dp2[i][j] = max(dp2[i+1][j], dp2[i][j+1]) + a[i][j];

dp3[i][j] = max(dp3[i+1][j], dp3[i][j-1]) + a[i][j];

dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + a[i][j];

程式碼如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pi acos(-1)
#define For(i, a, b) for(int (i) = (a); (i) <= (b); (i) ++)
#define Bor(i, a, b) for(int (i) = (b); (i) >= (a); (i) --)
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;
inline int read(){
    int ret=0,f=0;char ch=getchar();
    while(ch>'9'||ch<'0') f^=ch=='-',ch=getchar();
    while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
    return f?-ret:ret;
}
int a[maxn][maxn];
int dp1[maxn][maxn];
int dp2[maxn][maxn];
int dp3[maxn][maxn];
int dp4[maxn][maxn];
int main(){
	int n, m;
	while(~scanf("%d%d",&n,&m)){
		memset(dp1, 0, sizeof(dp1));
		memset(dp2, 0, sizeof(dp2));
		memset(dp3, 0, sizeof(dp3));
		memset(dp4, 0, sizeof(dp4));
		memset(a, 0, sizeof(a));
		For(i, 1, n)
			For(j, 1, m)
				scanf("%d",&a[i][j]);
		/*For(i, 1, n)
			For(j, 1, m)
				printf("%d%c",a[i][j], j == m ? '\n' : ' ');*/
				
				
		//左上 
		For(i, 1, n)
			For(j, 1, m)
				dp1[i][j] = max(dp1[i - 1][j], dp1[i][j - 1]) + a[i][j];
		//右下 
		Bor(i, 1, n)
			Bor(j, 1, m)
				dp2[i][j] = max(dp2[i + 1][j], dp2[i][j + 1]) + a[i][j];
		//左下 
		Bor(i, 1, n)
			For(j, 1, m)
				dp3[i][j] = max(dp3[i + 1][j], dp3[i][j - 1]) + a[i][j];
		//右上	
		For(i, 1, n)
			Bor(j, 1, m)
				dp4[i][j] = max(dp4[i - 1][j], dp4[i][j + 1]) + a[i][j];
			
		int ans = 0;
		For(i, 2, n - 1){
			For(j, 2, m - 1){
				ans = max(ans, dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]);
				ans = max(ans, dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]);
			}
		}
		printf("%d\n",ans);
	}	
	
	return 0;
}

 

繼續彌補自己的不足。