递推DP POJ 1163 The Triangle

 

题目传送门

 1 /*  2  数塔  3  自底向上  4 */  5 #include <cstdio>  6 #include <iostream>  7 #include <cstring>  8 #include <string>  9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 const int MAXN = 100 + 10; 14 const int INF = 0x3f3f3f3f; 15 int a[MAXN][MAXN]; 16 int dp[MAXN][MAXN]; 17 18 void work(int n) 19 { 20 for (int i=n-1; i>=1; --i) 21  { 22 for (int j=1; j<=i; ++j) 23  { 24 dp[i][j] += max (dp[i+1][j], dp[i+1][j+1]); 25  } 26  } 27 printf ("%d\n", dp[1][1]); 28 } 29 30 int main(void) //POJ 1163 The Triangle 31 { 32 //freopen ("H.in", "r", stdin); 33 34 int n; 35 while (~scanf ("%d", &n)) 36  { 37 for (int i=1; i<=n; ++i) 38  { 39 for (int j=1; j<=i; ++j) 40  { 41 scanf ("%d", &a[i][j]); 42 dp[i][j] = a[i][j]; 43  } 44  } 45  work (n); 46  } 47 48 return 0; 49 }

 

你可能感兴趣的:(poj)