来源:http://poj.org/problem?id=1163
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 37660 | Accepted: 22611 |
Description
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1)
Input
Output
Sample Input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
Source
题意: 找一条从顶节点到底部的路径,使该路径上点之和最大,求最大值。
题解: 动态规划解决之
AC代码:
#include<cstdio> #include<cstring> const int Max=105; int max[Max][Max],sum[Max][Max],n; int DP(int i,int j){ if(~sum[i][j]) return sum[i][j]; if(i==n-1) sum[i][j]=max[i][j]; else sum[i][j]=max[i][j]+(DP(i+1,j)>DP(i+1,j+1) ? DP(i+1,j):DP(i+1,j+1)); return sum[i][j]; } int main() { memset(sum,-1,sizeof(sum)); scanf("%d",&n); for(int i=0;i<n;i++) for(int j=0;j<=i;j++) scanf("%d",&max[i][j]); printf("%d\n",DP(0,0)); return 0; }
POJ 1163 The Triangle,布布扣,bubuko.com
原文地址:http://blog.csdn.net/mummyding/article/details/38709037