Working out 【codeforces-429B】【动态规划】

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

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
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的方格,A从左上角到右下角,B从左下角到右上角。要求两条线路权值和的最大值,其中两条路线有且只能交叉于一点,并且交叉处权值不计入总和。

题解:由题意可知,交叉点不在边缘处(因为只能交于一点),交叉点到四个角的总和的最大值即为所求。其中要特别注意的一点是:如下图所示,只能以这两种方式交叉。



代码如下:

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

const int maxn=1005;
int a[maxn][maxn];
int dpx1[maxn][maxn];
int dpx2[maxn][maxn];
int dpy1[maxn][maxn];
int dpy2[maxn][maxn];

int main()
{
	int n,m;
	while(~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(dpx1,0,sizeof(dpx1));
		memset(dpx2,0,sizeof(dpx2));
		memset(dpy1,0,sizeof(dpy1));
		memset(dpy2,0,sizeof(dpy2));
	
		for(int i=1;i<n;i++){
			for(int j=1;j<m;j++){
				dpx1[i][j]=max(dpx1[i-1][j],dpx1[i][j-1])+a[i][j];
			}
		}
	
		for(int i=n;i>1;i--){
			for(int j=m;j>1;j--){
				dpx2[i][j]=max(dpx2[i+1][j],dpx2[i][j+1])+a[i][j];
			}
		}	
	
		for(int i=n;i>1;i--){
			for(int j=1;j<m;j++){
				dpy1[i][j]=max(dpy1[i+1][j],dpy1[i][j-1])+a[i][j];
			}
		}
	
		for(int i=1;i<n;i++){
			for(int j=m;j>1;j--){
				dpy2[i][j]=max(dpy2[i-1][j],dpy2[i][j+1])+a[i][j];
			}
		}
	
		int res=-1;
		for(int i=2;i<n;i++){
			for(int j=2;j<m;j++){
				res=max(res,dpx1[i-1][j]+dpx2[i+1][j]+dpy1[i][j-1]+dpy2[i][j+1]);
				res=max(res,dpx1[i][j-1]+dpx2[i][j+1]+dpy1[i+1][j]+dpy2[i-1][j]);
			}
		}		
		printf("%d\n",res);
	}
	
	
	
	return 0;
}


发布了79 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/DNMTOOBA/article/details/79406989