hdu 2084 数塔 (基础dp)
题意:dp入门题。。。数字三角形。。
思路:
昨天看mit公开课。。。讲到dp的精髓是sub-problem+ reuse...
为什么自底向上呢。。。
初始化dp[n][i] = a[n][i]其实是在处理只有最后一行的子问题。。。
需要特别强调的是。。处于某个子问题的时候。。。其他部分就好像不存在一样。。。
每一个点只能向下或者向右下两条路可走。。。
那么对于这一点的最大值。。。一定是取后来可走的两点的最大值加上自身。。。
/* ***********************************************
Author :111qqz
Created Time :2016年07月27日 星期三 12时56分28秒
File Name :code/hdu/2084.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=105;
7int n;
8int a[N][N];
9int dp[N][N];
10int main()
11{
12 #ifndef ONLINE_JUDGE
13 freopen("code/in.txt","r",stdin);
14 #endif
15 int T;
16 cin>>T;
17 while (T--)
18 {
19 scanf("%d",&n);
20 ms(dp,0);
21 for ( int i = 1 ; i <= n ; i++)
22 for ( int j = 1 ; j <= i ; j++)
23 scanf("%d",&a[i][j]);
for ( int i = 1 ; i <= n ; i++)
dp[n][i] = a[n][i];
1 for ( int i = n-1 ; i >=1 ; i--)
2 for ( int j = 1 ; j <= i ; j++)
3 dp[i][j] = max(dp[i+1][j],dp[i+1][j+1]) + a[i][j];
printf("%d\n",dp[1][1]);
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}