4979 Count Tower
time limit: 1 sspace limit: 32000 KBtitle level: Golden Gold SolvingTitle Description
Description
There are several towers, starting from the top, choosing to go left or right on each node and go to the end. Requires finding a path that maximizes the sum of the paths.
13
11 8
12 7 26
6 14 15 8
12 7 13) 24 11
Enter a description
Input Description
The first line enters a positive integer n
Next, from line 2nd to line n+1, enter the number of rows per line minus 1 positive integer m.
Output description
Output Description
A total of two lines
The first line outputs the maximum sum;
The second line outputs the maximum path, separated by '-';
Sample input
Sample Input
5
13
11 8
12 7 26
6 14 15 8
12 7 13) 24 11
Sample output
Sample Output
86
13-8-26-15-24
Data range and Tips
Data Size & Hint
40% data: n<=20, m<=1000
60% data: n<=60, m<=1050
Category labels
Tags Click here to expandNo label
The following:Number triangle + Output path
AC Code:
#include <cstdio>#include<algorithm>using namespacestd;BOOLD;intb[101][101],c[101][101],n,tot;intMain () {inti,j; scanf ("%d",&N); for(i=1; i<=n;i++){ for(j=1; j<=i;j++) {scanf ("%d",&B[i][j]); C[I][J]=B[i][j]; } } for(i=n-1; i>=1; i--) for(j=1; j<=i;j++) B[i][j]+=max (b[i+1][j],b[i+1][j+1]); printf ("%d\n%d-", b[1][1],c[1][1]); J=1; for(intI=2; i<=n;i++){ if(b[i][j]>=b[i][j+1]) printf ("%d", C[i][j]); Else{printf ("%d", c[i][j+1]); J++; } if(i!=N) printf ("-"); } return 0;}
4979 Count Tower