Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
人生中的第一个DP题,mark#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int Max = 110; int d[Max][Max]; int a[Max][Max]; int n; int maxNum(int a, int b) { if(a > b) return a; else return b; } int solve(int i, int j) //求解d[i][j]的值,即从a[i][j]到底端能得到的最大的数 { //记忆化搜索,已经搜索过的,放进d数组中 if(d[i][j] > 0) //说明已经有值了,直接返回 { return d[i][j]; } else if(i == n) { return d[i][j] = a[i][j]; //如果是底层,直接赋给a[i][j] } else { return d[i][j] = a[i][j] + maxNum(solve(i+1,j),solve(i+1,j+1)); } } int main() { int i,j; while(scanf("%d",&n) != EOF) { memset(d,-1,sizeof(d)); for(i=0; i<n; i++) { for(j=0; j<=i; j++) { scanf("%d",&a[i][j]); } } printf("%d\n",solve(0,0)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator