Back file | Check number of squares

Source: Internet
Author: User

Title Description Description
with N*n (n<=9), we fill some of these squares with positive integers, while the other squares
Person number 0. As shown (see examples):
A
0 0 0 0 0 0 0 0
0 0 0 0 6 0 0
0 0 0 0 7 0 0 0
0 0 0 0 0 0 0
0 0 0 0 4 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
B
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 B
Point. 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.

input/output format Input/output
Input Format:
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
represents the position, and the third number is the number placed at that position. A single line of 0 indicates the end of the input.
output Format:
just output An integer that represents the maximum and the 2 paths that are obtained.
 
Input Sample:
8
2 3
2 6 6
3 5 7
4 4
5 2
5 6 4
6 3
7 2
0 0 0
Output Sample:


Interpretation of the topic: The key to this problem is the determination of the two-time path. It is undoubtedly the best choice not to change the coordinates of the map. So we only need to search the optimal path, then use dynamic programming to find the shortest path from the remaining lattice. (Worship Myf, this question can also use four-dimensional move to return but really do not want to write ... )

varN,i,j,answer,t,k:longint; A,map,p:Array[0.. One,0.. One] ofLongint;functionMax (a,b:longint): Longint;begin   ifA>b Thenexit (a); Exit (b); End;
procedureWork (x,y,ans:longint);begin if(x<1)or(y<1)or(x>n)or(y>n) Thenexit; if(x=n) and(y=n) Then beginP[n,n]:=0; ans:=ans+Map[x,y]; fori:=1 toN Do forj:=1 toN DoA[i,j]:=map[i,j]*P[i,j]; fori:=1 toN Do forj:=1 toN DoA[i,j]:=a[i,j]+max (a[i-1, j],a[i,j-1]); Answer:=max (answer,a[n,n]+ans); End; P[x,y]:=0; Work (x, Y+1, ans+Map[x,y]); Work (x+1, y,ans+Map[x,y]); P[x,y]:=1; End;
beginREADLN (n); fori:=1 toN Do forj:=1 toN Dop[i,j]:=1; whileTrue Do beginreadln (i,j,k); ifI=0 ThenBreak ; MAP[I,J]:=K; End; Answer:=0; Work (1,1,0); Writeln (answer); End.

Back file | Check number of squares

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.