Codeforces 429 b b. Working out

Source: Internet
Author: User

Description

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 O F Gym in the I-th line and the J-th column.

Iahub starts with workout located on line1 and column 1. He needs to the finish with Workout  a [ n ] [ m ]. After finishing Workout  a [ i ] [ J ], he can go to workout < Span class= "Tex-span" > 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& nbsp a [ i ] [ J + 1] or  a [ I -1][ J ].

There is one additional condition for their training. They has the meet in exactly one cell of gym. At that cell, none of them would work out. They'll talk on fast exponentiation (pretty odd small talk) and then both of the them of the would move to the next workout.

If a workout was did by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as all gain to be as big as possible. Note, that is Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach mee T cell may differs.

Input

The first line of the input contains the 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.

Sample inputinput3 3 -  -  - - 1  - -  -  -Output -Hintiahub'll choose Exercises a[1][1]→a[1][2]→a[2][2]→a[3][2]→a[3][3]. Iahubina'll choose Exercises a[3][1]→a[2][1]→a[2][2]→a[2][3]→a[1][3].

Title Link: Http://codeforces.com/problemset/problem/429/B

*********************************************************

Test instructions: Iahub and Iahubina exercise in a matrix gym, Iahub from (a) to reach (N,m), Iahubina from (n,1), to arrive (1,m); they both have to see the other side.

The idea of solving a problem: You can enumerate the maximum situation of each point that may meet

DP1[I][J] FROM (to) the maximum value (I,J)
DP2[I][J] The maximum value from (n,m) to (I,J)
DP3[I][J] The maximum value from (n,1) to (I,J)
DP1[I][J] The maximum value from (1,m) to (I,J)

Ans For the end result:
Since each point of the traversal is, it can be divided into four parts
(i,j) + (I,J), (n,m) + (n,1), (i,j) + (1,m), (I,J)

It is therefore possible to update the maximum value of part four with ans

AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<map>#include<vector>using namespacestd;#defineN 1200#defineINF 0x3f3f3f3fintMaps[n][n],dp1[n][n],dp2[n][n],dp3[n][n],dp4[n][n];intMain () {intn,m,i,j;  while(SCANF ("%d%d", &n,&m)! =EOF) {memset (DP1,0,sizeof(DP1)); memset (DP2,0,sizeof(DP2)); memset (DP3,0,sizeof(DP3)); memset (DP4,0,sizeof(DP4)); memset (Maps,0,sizeof(maps));  for(i=1; i<=n; i++)             for(j=1; j<=m; J + +) scanf ("%d", &Maps[i][j]);  for(i=1; i<=n; i++)///( max) to (I,J) maximum value             for(j=1; j<=m; J + +) Dp1[i][j]=max (dp1[i-1][j],dp1[i][j-1])+Maps[i][j];  for(I=n; i>=1; i--)///(N,m) to (I,J) maximum value             for(J=m; j>=1; j--) Dp2[i][j]=max (dp2[i+1][j],dp2[i][j+1])+Maps[i][j];  for(I=n; i>=1; i--)///(n,1) to (I,J) maximum value             for(j=1; j<=m; J + +) Dp3[i][j]=max (dp3[i][j-1],dp3[i+1][J]) +Maps[i][j];  for(i=1; i<=n; i++)///(1,m) to (I,J) maximum value             for(J=m; j>=1; j--) Dp4[i][j]=max (dp4[i][j+1],dp4[i-1][J]) +Maps[i][j]; intans=0;  for(i=2; i<n; i++)             for(j=2; j<m; J + +) {ans=max (ans,dp1[i][j-1]+dp2[i][j+1]+dp3[i+1][j]+dp4[i-1][j]); Ans=max (ans,dp1[i-1][j]+dp2[i+1][j]+dp3[i][j-1]+dp4[i][j+1]); }///( i,j) + (I,J), (n,m) + (n,1) , (i,j) + (1,m), (I,J)printf ("%d\n", ans); }    return 0;}

Codeforces 429 b b. Working out

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.