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