Number of DP squares to be taken

Source: Internet
Author: User

Title Description Description

A square chart with n*n (n<=10, we fill in some of these squares with positive integers, while the other squares put the number 0. As shown (see examples):

Someone from a point in the upper left corner of the picture, you can walk down, or to the right, until you reach the lower right corner of point B. On the way, he can take the number in the squares (the squares will change to the number 0).

This person from point A to point B to walk two times, try to find out 2 such paths, so that the sum of the obtained number is the largest.

Enter a description Input Description

The first behavior of the input is an integer n (a square chart representing n*n), followed by three integers per line, the first two representing the position, and the third number as the number placed on that position. A single line of 0 indicates the end of the input.

Output description Output Description

Just output an integer that represents the maximum and the 2 paths that are obtained.

Sample input Sample Input

8

2 3 13

2 6 6

3 5 7

4 4 14

5 2 21

5 6 4

6 3 15

7 2 14

0 0 0

Sample output Sample Output

67

var init:array[0..10,0..10]of longint;
F:array[0..10,0..10,0..10,0..10]of Longint;
I,j,k,l:longint;
X,y,v,n:longint;
Max:longint;

Begin Fillchar (init,sizeof (init), 0);
Fillchar (F,sizeof (f), 0);
READLN (n);
READLN (X,Y,V);
while (x<>0) do
Begin Init[x,y]:=v;
READLN (X,Y,V);
End
For I:=1 to N do
For J:=1 to N do
For K:=1 to N do
For L:=1 to N do
Begin max:=0;
If F[i-1,j,k-1,l]>max
Then max:=f[i-1,j,k-1,l];
If F[i,j-1,k-1,l]>max
Then max:=f[i,j-1,k-1,l];
If F[i-1,j,k,l-1]>max
Then MAX:=F[I-1,J,K,L-1];
If F[i,j-1,k,l-1]>max
Then MAX:=F[I,J-1,K,L-1];
if (i=k) and (j=l)
Then Max:=max+init[i,j]
else max:=max+init[i,j]+init[k,l];
F[i,j,k,l]:=max;
End
Writeln (F[n,n,n,n]);
End.

Number of DP squares to be taken

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.