Shuta |
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others) |
Total submission (s): 106 accepted submission (s): 94 |
|
Problem descriptionDescribe DPAlgorithmA classic example is the data tower problem. It is described as follows: There is a number tower as shown below. It is required to go from the top layer to the bottom layer. If each step can only go to adjacent nodes, what is the maximum sum of the numbers of the nodes that pass through?
I already told you that this is a DP question. Can you AC it? |
InputThe input data first includes an integer c, indicating the number of test instances. The first row of each test instance is an integer N (1 <= n <= 100 ), the height of the tower. Next, use N rows of numbers to represent the tower. Row I has an I integer, and All integers are within the range [0, 99. |
OutputFor each test instance, the output is the largest possible sum, and each instance occupies one row of output. |
Sample Input1573 88 1 0 2 7 4 44 5 2 6 5 |
# Include <iostream>
Using namespaceSTD;
Int Main ()
{
Int C , N , D [ 101 ] [ 101 ], A [ 101 ] [ 101 ], H ;
CIN>C;
While(C--)
{
CIN>N;
H = N ;
While ( N --)
{
For ( Int I = 1 ; I <= ( H- N ); I ++)
{
CIN > A [ H - N ] [ I];
}
}
For ( Int I = H ; I > = 1 ; I --)
For ( Int J = 1 ; J <= I ; J ++)
{
If ( I = H ) D [ I ] [ J ] = A [ I ] [ J ];
Else
D [ I ] [ J ] = A [ I ] [ J ] + ( D [ I + 1 ] [ J]> D [ I + 1 ] [ J + 1 ]? D [ I + 1 ] [ J]: D [ I + 1 ] [ J + 1 ]);
}
cout d [ 1 ] [ 1 ] Endl ;< BR >}
return 0 ;
}