Harbin University of Science and technology OJ on the topic, water is very water, the result I did a long time, found in the problem to be recycled input, I was drunk.
The topic is very simple but the heart is unbalanced decide to send.
Description School Road is always so hard, in small lele hard out of the house, she just remembered her home has moved to the mountain (really confused). The road down the mountain is also very difficult, different places of the ease of access is different. , Little Lele now on the top of the hill, she has two roads, each road to a location, each location has a value, indicating the ease of the passage of the degree. The bottom of the site is the foot of the hill. You know, little lele good lazy lazy, she wants to know how to climb the most labor? Input first line an integer n (1<n<500)
Then n lines, line i+1, I have a number
Indicates the road traffic output from the peak to the foot of the mountain the most labor-saving way. (Guaranteed answer only) Sample Input
3
1
2 5
5 6 3
Sample Output
1 2 5
Hint input is a triangle, each point can go to the following point and lower right point.
1#include <stdio.h>2#include <math.h>3#include <string.h>4#include <iostream>5 using namespacestd;6 inta[ -][ -];7 intdp[ -][ -];8 intN;9 intDpit (intIintj)Ten { One if(i==N) A returndp[i][j]=A[i][j]; - Else - returnDp[i][j]=a[i][j]+min (Dpit (i+1, j), Dpit (i+1, j+1)); the } - voidPrintintIintj) - { - if(i>n)return ; + if(dp[i][j]>dp[i][j+1]) - { +printf"%d", a[i][j+1]); A if(i!=N) atprintf" "); - Else -printf"\ n"); -Print (i+1, j+1); - } - Else in { -printf"%d", A[i][j]); to if(i!=N) +printf" "); - Else theprintf"\ n"); * $Print (i+1, j);Panax Notoginseng } - } the intMain () + { A while(~SCANF ("%d",&N)) { the for(intI=1; i<=n; i++) + for(intj=1; j<=i; J + +) -scanf"%d",&a[i][j]); $Memset (DP,0,sizeof(DP)); $ //printf ("%d", dp[1][1]); - //int Ans=dpit (+); - for(intI=n; i>=1; i--) the for(intj=1; j<=i; J + +) - {Wuyi if(i==N) thedp[i][j]=A[i][j]; - //printf ("%d", Dp[i][j]);} Wu Else -Dp[i][j]=a[i][j]+min (dp[i+1][j],dp[i+1][j+1]); About //printf ("%d%d%d i=%d j=%d\n", dp[i+1][j],dp[i+1][j+1],a[i][j],i,j); $ /*printf ("%d", dp[i][j]);*/ - } - /*for (int i=1;i<=n;i++) - for (int j=1;j<=n;j++) { A printf ("%3d", Dp[i][j]); + if (j==n) the printf ("\ n"); - }*/ $printf"%d", a[1][1]); thePrint2,1); the } the}
The need to record the path, we have to find the path, anyway, the DP process has been saved, so according to the state transfer equation we can find the path again, is to go to a small place.
Dynamic programming Path Output method There are a lot of things that I will learn.
Little Lele to go down the mountain (brain debris dynamic planning record Path)