7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
Input
Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.
Output
Your program is to write to standard output. The highest sum is written as an integer.
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30

这道题若是从上往下搜索会有2^n条路径,复杂度很高,但若是换个思路,从下往上搜索,复杂度为O(n^2)。
直接dp代码:

include

include

using namespace std;
int main()
{
int n;
cin>>n;
int dp[110][110]={0};
int a[110][110]={0};
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
cin>>a[i][j];
for(int i=n;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;
}

如果非要从上往下搜索,可以用递推加记忆化搜索,复杂度O(n^2):

include

include

using namespace std;
int a[110][110]={0},dp[110][110];
int n;
int dfs(int i,int j)
{
if(i==n) return a[i][j];
if(dp[i][j]>=0) return dp[i][j];
dp[i][j]=max(dfs(i+1,j),dfs(i+1,j+1))+a[i][j];
return dp[i][j];
}
int main()
{
cin>>n;
memset(dp,-1,sizeof(dp));
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
cin>>a[i][j];
dfs(1,1);
cout<<dp[1][1]<<endl;
return 0;
}

poj1163The Triangle(动态规划,记忆化搜索)的更多相关文章

  1. sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

    Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...

  2. Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)

    描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提 ...

  3. Poj-P1088题解【动态规划/记忆化搜索】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=1088 题目描述: 区域由一个二维数组给 ...

  4. UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)

    描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. 滑雪---poj1088(动态规划+记忆化搜索)

    题目链接:http://poj.org/problem?id=1088 有两种方法 一是按数值大小进行排序,然后按从小到大进行dp即可: #include <iostream> #incl ...

  6. [NOIP2017] 逛公园 (最短路,动态规划&记忆化搜索)

    题目链接 Solution 我只会60分暴力... 正解是 DP. 状态定义: \(f[i][j]\) 代表 \(1\) 到 \(i\) 比最短路长 \(j\) 的方案数. 那么很显然最后答案也就是 ...

  7. 动态规划——I 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  8. 动态规划——数字三角形(递归or递推or记忆化搜索)

    动态规划的核心就是状态和状态转移方程. 对于该题,需要用抽象的方法思考,把当前的位置(i,j)看成一个状态,然后定义状态的指标函数d(i,j)为从格子出发时能得到的最大和(包括格子本身的值). 在这个 ...

  9. Vijos 1011 清帝之惑之顺治 记忆录式的动态规划(记忆化搜索)

    背景 顺治帝福临,是清朝入关后的第一位皇帝.他是皇太极的第九子,生于崇德三年(1638)崇德八年八月二ten+six日在沈阳即位,改元顺治,在位18年.卒于顺治十八年(1661),终24岁. 顺治即位 ...

随机推荐

  1. Webstorm上已有的本地项目上传到Github

    1.Webstorm左上角File —— Settings—— Github, Auth Type改成password,在Login和Password中输入自己的github账户和密码,点Test测试 ...

  2. Robomongo可视化命令

    ## 更新命令示例 db.getCollection('collection_name').update(    {'_id':ObjectId('id string')},     {$set:{' ...

  3. css3-css3属性选择器

    在HTML中,通过各种各样的属性可以给元素增加很多附加的信息.例如,通过id属性可以将不同div元素进行区分. 在CSS2中引入了一些属性选择器,而CSS3在CSS2的基础上对属性选择器进行了扩展,新 ...

  4. LOJ 2840「JOISC 2018 Day 4」糖

    有趣的脑子题(可惜我没有脑子 好像也可以称为模拟费用流(? 我们考虑用链表维护这个东西 再把贡献扔到堆里贪心就好了 大概就是类似于有反悔机制的贪心?我们相当于把选中的一个打上一个-v的tag然后如果选 ...

  5. python 小工具 重命名当前文件夹内所有的文件,升序命名

    背景:一个朋友想升序重命名他的照片,但是太多了不想手动所以,emememem os这个模块,不用说,rename,filedir等 #conding=utf8 import os path = os. ...

  6. python语句执行

    python文件中的语句,按顺序执行,执行import时,原文件会入栈,等import文件执行完成后,才会出栈执行. load/const.py --- import os DB_ADDRESS = ...

  7. NLP第一周

    19-21周,每周学习15小时以上 基础:Python编程基础:基础的概览统计.了解线性代数:足够的时间投入. 完成9个课程项目,每个5小时-15小时 完成聊天机器人项目(40-80小时) Capst ...

  8. vue对特殊特性的研究

    key 预期:number | string key 的特殊属性主要用在 Vue 的虚拟 DOM 算法,在新旧 nodes 对比时辨识 VNodes.如果不使用 key,Vue 会使用一种最大限度减少 ...

  9. NVMe固态硬盘工具箱使用说明

    https://www.bilibili.com/read/cv562989/ 浦科特NVMe固态硬盘工具箱使用说明 数码 2018-6-7 687阅读7点赞3评论 浦科特已经推出针对NVMe固态硬盘 ...

  10. Transaction 在同一个类中不生效

    参考:https://blog.csdn.net/qq_30336433/article/details/83338835 最近在开发项目中踩到一个坑,以此记录下来.以备后来人借鉴 1.相信使用spr ...