HDU 2084 数塔 (经典dp)
2015-02-07 23:21
330 查看
题目链接:轻点我一下 >3<
从下往上的姿势特别好 - - 其实可以优化下空间 直接把读入到dp数组中就可以了 懒得改了写的有点久了 然后状态方程变为dp[i][j] += max(dp[i + 1][j], dp[i + 1][j + 1])
AC代码如下://
// HDU 2084 数塔
//
// Created by TaoSama on 2015-01-23
// Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x) memset(x, 0, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e3 + 10;
int n, a[105][105], dp[105][105];
int main() {
#ifdef LOCAL
freopen("ACM_in.txt", "r", stdin);
//freopen("ACM_out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; cin >> t;
while(t--) {
cin >> n;
memset(dp, 0xc0, sizeof dp);
for(int i = 1; i <= n; ++i) for(int j = 1; j <= i; ++j) {
cin >> a[i][j];
if(i == n) dp[i][j] = a[i][j];
}
for(int i = n - 1; i > 0; --i) for(int j = 1; j <= i; ++j) {
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + a[i][j];
}
cout << dp[1][1] << endl;
}
return 0;
}
从下往上的姿势特别好 - - 其实可以优化下空间 直接把读入到dp数组中就可以了 懒得改了写的有点久了 然后状态方程变为dp[i][j] += max(dp[i + 1][j], dp[i + 1][j + 1])
AC代码如下://
// HDU 2084 数塔
//
// Created by TaoSama on 2015-01-23
// Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x) memset(x, 0, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e3 + 10;
int n, a[105][105], dp[105][105];
int main() {
#ifdef LOCAL
freopen("ACM_in.txt", "r", stdin);
//freopen("ACM_out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; cin >> t;
while(t--) {
cin >> n;
memset(dp, 0xc0, sizeof dp);
for(int i = 1; i <= n; ++i) for(int j = 1; j <= i; ++j) {
cin >> a[i][j];
if(i == n) dp[i][j] = a[i][j];
}
for(int i = n - 1; i > 0; --i) for(int j = 1; j <= i; ++j) {
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + a[i][j];
}
cout << dp[1][1] << endl;
}
return 0;
}
相关文章推荐
- hdu 2084 数塔(水) 经典简单DP
- HDU 2084 DP经典例子---数塔问题
- 数塔 HDU - 2084【经典DP】
- hdu 2084(数塔-经典dp)
- hdu2084 - 数塔 (经典dp)
- 各类经典dp问题分讲(HDU 2084、HDU 1003、HDU 1176)
- hdu 2084(数塔-经典dp)
- HDU 2084 数塔(经典dp)
- [经典dp] HDU - 2517 棋盘分割 [废话流详解]
- [HDU] 2084 数塔 - 入门dp
- hdu 2084 DP
- hdu 1024 Max Sum Plus Plus (最大m子段和)(经典DP)(转)
- hdu 1024 Max Sum Plus Plus (最大m子段和)(经典DP)(转)
- hdu 1003/poj 1050 连续子序列最大和经典基础dp
- HDU 2196 Computer(树形dp经典)
- 【DP复习5】HDU 2084——数塔
- HDU 1024 Max Sum Plus Plus DP中的经典
- hdu 4616 经典树形dp 多校第二场
- [经典dp] HDU - 2517 棋盘分割 [废话流详解]
- HDU 1516 String Distance and Transform Process(经典DP)