Write a program to find the path in the end the end of the unit anywhere from the highest point, and the path through the largest numbers. Each step can go to the lower left point can also reach the lower right point.
Analysis
inverse extrapolation
row partitioning phase triangle, if the number of lines is n, the decision problem can be seen as a problem to the n-1 stage. First stage n-1 is obtained (the points of the n-1 lines) to a maximum and then sequentially obtains stage n-2 n-th row, the first stage n-3 ...... first stage (onset ) optimal path to each decision point of the n-th row.
Provided: F [i, j] the largest number of the i-th stage point j to the n-th row and the state transition equation is:
then: f [n, j] = a [n, j] 1 <= J <= n-
F [I, J] = max {A [I, J] + F [I +. 1, J], A [I, J] + F [I +. 1, J +. 1]}. 1 <= J <= I,. 1 <= I <=. 1-n-
F [1,1] is also desired.
var
n-, I, J: LongInt;
A, F: Array [1..1000,1..1000] of LongInt;
the begin
Readln (n-);
for I: =. 1 to n-do
for J: = I do. 1 to
Read (A [I, J]);
for I: =. 1 to n-do
F [n-, I]: = A [n-, I];
for I: = n--. 1 downto. 1 do
for J: =. 1 to I do
if f[i+1,j]>f[i+1,j+1] then f[i,j]:=a[i,j]+f[i+1,j] else f[i,j]:=a[i,j]+f[i+1,j+1];
write(f[1,1]);
end.
Digital algorithms analyze the triangle of Converse
Guess you like
Origin www.cnblogs.com/khnl/p/11639264.html
Recommended
Ranking