http://acm.hdu.edu.cn/showproblem.php?pid=2084

题意:

        7
      3      8
    8     1       0
  2       7     4       4
4    5    2       6        5
在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大。
路径上的每一步都只能往左下或右下走。只需要求出这个最大和即可,不必给出具体路径。
 
解法1:dfs搜索每一条路径,可以发现很多点会重复搜索。时间复杂度为:O(2的n次方)
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include<time.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[109][109];
int n ; int getsum(int i , int j)
{
if(i == n)
return a[i][j];
else{
int x = getsum(i+1 , j);
int y = getsum(i+1 , j+1);
return max(x , y) + a[i][j];
}
} int main()
{
/*#ifdef ONLINE_JUDGE
#else
freopen("D:/c++/in.txt", "r", stdin);
freopen("D:/c++/out.txt", "w", stdout);
#endif*/
int t ;
scanf("%d" , &t);
while(t--)
{
memset(dp , -1 , sizeof(dp));
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
scanf("%d" , &a[i][j]);
}
}
cout << getsum(1 , 1) << endl ; } return 0;
}

解法1改进:记忆化搜索,将搜索过的点记录下来。时间复杂度O(n的平方)。

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include<time.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[109][109];
int n ;
int dp[109][109]; int getsum(int i , int j)
{
if(i == n)
return a[i][j];
else{
if(dp[i][j] != -1) return dp[i][j];
int x = getsum(i+1 , j);
int y = getsum(i+1 , j+1);
dp[i][j] = max(x,y)+a[i][j];
return max(x , y) + a[i][j];
}
return dp[i][j] ;
} int main()
{
/*#ifdef ONLINE_JUDGE
#else
freopen("D:/c++/in.txt", "r", stdin);
freopen("D:/c++/out.txt", "w", stdout);
#endif*/
int t ;
scanf("%d" , &t);
while(t--)
{
memset(dp , -1 , sizeof(dp));
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
scanf("%d" , &a[i][j]);
}
}
cout << getsum(1 , 1) << endl ;
} return 0;
}

解法2、递归转为递推。从底层向上递推。

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include<time.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[109][109];
int n ;
int dp[109][109]; int main()
{
/*#ifdef ONLINE_JUDGE
#else
freopen("D:/c++/in.txt", "r", stdin);
freopen("D:/c++/out.txt", "w", stdout);
#endif*/
int t ;
scanf("%d" , &t);
while(t--)
{
memset(dp , -1 , sizeof(dp));
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
scanf("%d" , &a[i][j]);
}
}
for(int i = 1 ; i <= n ; i++)
{
dp[n][i] = a[n][i];
}
for(int i = n - 1 ; i >= 1 ; 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;
}

解法2优化空间

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include<time.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[109][109];
int n ;
int dp[109]; int main()
{
/*#ifdef ONLINE_JUDGE
#else
freopen("D:/c++/in.txt", "r", stdin);
freopen("D:/c++/out.txt", "w", stdout);
#endif*/
int t ;
scanf("%d" , &t);
while(t--)
{
memset(dp , -1 , sizeof(dp));
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
scanf("%d" , &a[i][j]);
}
}
for(int i = 1 ; i <= n ; i++)
{
dp[i] = a[n][i];
}
for(int i = n - 1 ; i >= 1 ; i--)
{
for(int j = 1 ; j <= i ; j++)
{
dp[j] = max(dp[j] , dp[j+1])+a[i][j];
}
}
cout << dp[1] << endl ;
} return 0;
}

dp入门题(数塔)的更多相关文章

  1. 【dp入门题】【跟着14练dp吧...囧】

    A HDU_2048 数塔 dp入门题——数塔问题:求路径的最大和: 状态方程: dp[i][j] = max(dp[i+1][j], dp[i+1][j+1])+a[i][j];dp[n][j] = ...

  2. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  3. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  4. (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520

    题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...

  5. 数字三角形/数塔问题(DP入门题)

    有形如下图所示的数塔,从顶部出发,在每一结点可以选择向左走或是向右走,一起走到底层,要求找出一条路径,使路径上的值最大. 样例输入: 5 13 11 8 12 7 26 6 14 15 8 12 7 ...

  6. HDU2084 数塔 (DP入门题)

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  7. HDU 2089 不要62【数位DP入门题】

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. Hdu-1565 方格取数(1) (状态压缩dp入门题

    方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. hdu_Anniversary party_(树形DP入门题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:有N个人,N-1个人有自己的上司,每个人有一个快乐值,如果这个人参加了聚会,那么这个人的直 ...

随机推荐

  1. AcWing 107. 超快速排序(归并排序 + 逆序对 or 树状数组)

    在这个问题中,您必须分析特定的排序算法----超快速排序. 该算法通过交换两个相邻的序列元素来处理n个不同整数的序列,直到序列按升序排序. 对于输入序列9 1 0 5 4,超快速排序生成输出0 1 4 ...

  2. 「WC 2007」剪刀石头布

    题目链接 戳我 \(Solution\) 直接求很明显不太好求,于是考虑不构成剪刀石头布的情况. 我们现在假设一个人\(i\)赢了\(x\)场,那么就会有\(\frac{x*(x-1)}{2}\) 我 ...

  3. char、varchar 哪种的搜索效率高

    在MySQL 中char 和 varchar 都是存储字符串的,区别在于char有固定的长度,而varchar属于可变长的字符类型.char(M)类型的数据列里,每个值都占用M个字节,如果某个长度小于 ...

  4. Wordpress可以用来做什么?

    WordPress本身只是一款开源的.基于PHP的博客软件,但是由于WordPress的源码开源.结构优良.插件丰富.主题繁多,以至于是 WordPress成为世界上最流行的博客程序.<Word ...

  5. 全面解读php-引用变量(&)

    本文讲述引用传值的核心原理,看完即可扫清一切和引用传值相关的内容,不会了记得画图. 一.memory_get_usage的使用 传值赋值 // 定义一个变量 $a = range(0, 10000); ...

  6. windos 启动redis服务端与客户端

    服务端:1-win+R 打开命令行2-cd至redis目录,例如 G:\Redis63813-输入 redis-server.exe redis.windows.conf观察是否如图1:至此,已成功: ...

  7. 浏览器端-W3School-HTML:HTML DOM Style 对象

    ylbtech-浏览器端-W3School-HTML:HTML DOM Style 对象 1.返回顶部 1. HTML DOM Style 对象 Style 对象 Style 对象代表一个单独的样式声 ...

  8. idea报错及解决

    <b>root project 'test2': Web Facets/Artifacts will not be configured properly</b>Details ...

  9. Go语言引用类型

    切片 1.切片定义 a) 声明一个切片 , , } , , } b) 通过make来创建切片 ) c) 通过 := 语法来定义切片 slice := []int{} slice := make([], ...

  10. Win10 安装LoadRunner11遇到的问题及解决方案

    由于以用户或者管理员身份执行setup.exe都不能正常安装,如下截图是异常信息.尝试了网上很多修改本地组策略的方法,还是不行,最后只能通过DOS命令来执行setup.exe.