joj2511: Number triangle
2511: Number triangle
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
1s | 65536K | 467 | 233 | Standard |
5 3 4 8 1 2 5 4 3 6 2 1 7 9 8
A number trangle is composed of N(N<=100) line numbers, the i-th line contains i positive integers(<=100). A chess can walk from the top line of the triangle to the bottom line. Suppose the chess is on the k-th number of one line, then it can only move to the k-th number or the (k+1)-th number of the line below in one step. Find a path from the top to the bottom, which can maximize the sum of the integers on the path.
Input
There are multiple test cases.For each test case, there's an integer N representing the size of the triangle, followed by N lines of positive intergers, the first line has 1 integer, next has two integers... the Nth line has N integers.
Output
The maximum sum of the integers on the path.
Sample Input
3 5 7 0 2 4 3 3 2 6 1 2 3 9
Sample Output
16 12
Problem Source: xwbsw
This problem is used for contest: 120
Submit / Problem List / Status / Discuss
1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105][105]; 5 6 int main() 7 { 8 //freopen("in.txt", "r", stdin); 9 10 int n, i, j; 11 12 while (scanf("%d", &n) == 1) 13 { 14 memset(a, 0, sizeof(a)); 15 for (i=1; i<=n; ++i) 16 { 17 for (j=1; j<=i; ++j) 18 { 19 scanf("%d", &a[i][j]); 20 } 21 } 22 23 for (i=n; i>1; --i) 24 { 25 for (j=i; j>=1; --j) 26 { 27 if(a[i][j]+a[i-1][j]>a[i][j+1]+a[i-1][j]) 28 a[i-1][j]=a[i][j]+a[i-1][j]; 29 else 30 a[i-1][j]=a[i][j+1]+a[i-1][j]; 31 } 32 } 33 printf("%d\n", a[1][1]); 34 } 35 36 return 0; 37 }