Rokua 1508--likecloud-eating, eating and eating (multidimensional dynamic programming)

Source: Internet
Author: User

Topic background

In the advent of adolescence, why things.

Answer: "hyperthyroidism, hyperthyroidism, again hyperthyroidism; starve, starve, and starve." ”

Title Description

The Li Buffalo, which is in a certain period of time, has been in a state of starvation recently because of a more developed digestive system. One day in class, just as he was so hungry, he suddenly flashed a giant big table with a n*m (n and m<=200) moment, and was at the midpoint of the side of the big table. The table was divided to n*m a small square, each of which had a large, round, large dinner plate filled with food that made the Li Buffalo yearn. Li Buffalo has divided all the food on the table according to the amount of energy it can provide (some of it is negative, because it has to be pulled), he decides to eat it from his place to the other side of the table, but he eats a habit-eat only food in front of himself or in front of the left or right in front of the plate.

Since the Li Buffalo is hungry and wants to get the most energy, he has given you the problem.

The starting point for each set of data is below the middle position of the last line.

Input/output format

Input format:
[Input data:]

The first behavior is M n. (n is odd), Li Buffalo starts at the bottom of the last line in the middle of the

Next is the digital array of the m*n.

A total of M rows, n digits per line. The numbers are separated by spaces. Represents the energy provided by the food on the plate of the lattice.

The numbers are all integers.

Output format:
[Output data:]

A number that is the maximum energy value you find.

Input/Output sample

Input Sample # #:
6 7
16 4 3 12 6 0 3
4-5 6 7 0 0 2
6 0-1-2 3 6 8
5 3 4 0 0-2 7
-1 7 4 0 7-5 6
0-1 3 4 12 4 2
Sample # # of output:
41

Obviously a DP problem.
DP type: A[i,j]:=max (a[i,j]+a[i-1,j],a[i,j]+a[i-1,j-1],a[i,j]+a[i-1,j+1]);
That means DP from three directions.
Last from three directions to the A[n, (m+1) Div 2],a[n, (m+1) Div 2+1],a[n, (m+1) div 2-1] in the maximum value

The code is as follows:

var n,m,i,j:longint;
A:ARRAY[0..201,0..201] of Longint;
function Max (a,b,c:longint): Longint;
    Begin if (c>b) and (C>a) then exit (c);
    if (a>b) and (A>c) then exit (a);
if (B>a) and (B>c) then exit (b);
End
    Begin READLN (N,M);
        For I:=1 to n does begin for j:=1 to M do read (A[i,j]);
    READLN;
    End
    For i:=2 to N does for j:=1 to M do A[i,j]:=max (a[i,j]+a[i-1,j],a[i,j]+a[i-1,j-1],a[i,j]+a[i-1,j+1]);
Writeln (Max (A[n, (m+1) Div 2],a[n, (m+1) Div 2+1],a[n, (m+1) div 2-1])); End. 

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.