声明

本文不介绍dfs、dp算法的基础思路,有想了解的可以自己找资源学习。

本文适合于刚刚接触dfs和dp算法的人,发现两种算法间的内在联系。

本人算法之路走之甚短,如果理解出现问题欢迎大家的指正,我会分享基于我目前理解到的算法思想。

dfs与dp的关系

很多情况下,dfs和dp两种解题方法的思路都是很相似的,这两种算法在一定程度上是可以互相转化的。

想到dfs也就常常会想到dp,当然在一些特定的适用场合下除外。

dp主要运用了预处理的思想,而dfs则是类似于白手起家,一步步探索。一般来讲,能够预处理要好些,好比战争前做好准备。

dfs和dp都是十分重要的基础算法,在各个竞赛中都有涉及,务必精通。

经典例题-数字三角形 - POJ 1163

题目

The Triangle

Description

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

dfs思路


解题思路

自顶向下,将每种路径都走一遍。

通过迭代计算到最后一层,记录最后一层的所有值。

最后一层中的最大值即为所求。

具体代码

#include <iostream>
// use vector vessel to write down the final level
#include <vector>
// use 'sort' method in <algorithm>
#include <algorithm>
// use 'greater<T>' functional template in <functional>
#include <functional>
using namespace std;
// the maximum of the triangle ordinal
const int max_ordinal = 100;
// the depth
int num_of_rows;
// save data
int data[max_ordinal][max_ordinal];
// save the data of the final level
vector<int> ans; void dfs(int level, int sum, int column)
{
// avoid multi calculate
int current_sum = sum+data[level][column];
// save data which was in final level
if(level+1 == num_of_rows)
{
ans.push_back(current_sum);
return;
}
// binary tree
dfs(level+1, current_sum, column);
dfs(level+1, current_sum, column+1);
} int main()
{
cin >> num_of_rows;
for(int i = 0; i < num_of_rows; i++)
for(int j = 0; j <= i; j++)
scanf("%d", &data[i][j]);
dfs(0, 0, 0);
cout << "output data:" << endl;
sort(ans.begin(), ans.end(), greater<int>());
for(int i = 0; i < ans.size(); i++)
{
cout << ans[i] << "\t";
if(!((i+1) % 5)) cout << endl;
}
cout << endl; return 0;
}

dp思路


解题思路

dfs的思路是从上到下,而dp的思路是:从第二层开始,每下去一次往回看一下并取上一层相邻两个大的那个。

具体代码

#include <iostream>
// use 'max' method and 'sort' method
#include <algorithm>
// use 'greater<T>' functional template
#include <functional>
using namespace std;
// same as DFS
const int max_ordinal = 100;
int num_of_rows;
int data[max_ordinal][max_ordinal];
// the array of the dp method
int dp[max_ordinal][max_ordinal]; int main()
{
cin >> num_of_rows;
for(int i = 0; i < num_of_rows; i++)
for(int j = 0; j<= i; j++)
scanf("%d", &data[i][j]);
// dp now
dp[0][0] = data[0][0];
for(int i = 1; i < num_of_rows; i++)
{
for(int j = 0; j <= i; j++)
{
if(j-1 >= 0)
{
dp[i][j] = data[i][j] + max(dp[i-1][j], dp[i-1][j-1]);
} else {
dp[i][j] = data[i][j] + dp[i-1][j];
}
}
}
// calling 'sort' method
sort(dp[num_of_rows-1], &dp[num_of_rows-1][num_of_rows], greater<int>());
for(int i = 0; i < num_of_rows; i++)
cout << dp[num_of_rows-1][i] << " ";
cout << endl;
cout << "answer is: ";
cout << dp[num_of_rows-1][0] << endl; return 0;
}

dfs与dp算法之关系与经典入门例题的更多相关文章

  1. DFS与DP算法

    名词解释: DFS(Dynamic Plan):动态规划 DFS(Depth First Search):深度优先搜索 DFS与DP的关系 很多情况下,dfs和dp两种解题方法的思路都是很相似的,这两 ...

  2. dp算法之硬币找零问题

    题目:硬币找零 题目介绍:现在有面值1.3.5元三种硬币无限个,问组成n元的硬币的最小数目? 分析:现在假设n=10,画出状态分布图: 硬币编号 硬币面值p 1 1 2 3 3 5 编号i/n总数j ...

  3. POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)

    1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...

  4. 图片大小以及dp和px关系一览表,logo尺寸

    图片大小以及dp和px关系一览表 说明:根据上表,我们应该很容易算出一张图片在不同手机上的宽和高是多少. 结论 从上表可以得出如下结论 1. 图片放在drawable中,等同于放在drawable-m ...

  5. UvaLive6661 Equal Sum Sets dfs或dp

    UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. //#pr ...

  6. 0-1背包的动态规划算法,部分背包的贪心算法和DP算法------算法导论

    一.问题描述 0-1背包问题,部分背包问题.分别实现0-1背包的DP算法,部分背包的贪心算法和DP算法. 二.算法原理 (1)0-1背包的DP算法 0-1背包问题:有n件物品和一个容量为W的背包.第i ...

  7. 最大子段和的DP算法设计及其效率测试

    表情包形象取自番剧<猫咪日常> 那我也整一个 曾几何时,笔者是个对算法这个概念漠不关心的人,由衷地感觉它就是一种和奥数一样华而不实的存在,即便不使用任何算法的思想我一样能写出能跑的程序 直 ...

  8. 华为笔试——C++平安果dp算法

    题目:平安果 题目介绍:给出一个m*n的格子,每个格子里有一定数量的平安果,现在要求从左上角顶点(1,1)出发,每次走一格并拿走那一格的所有平安果,且只能向下或向右前进,最终到达右下角顶点(m,n), ...

  9. C++数字三角形问题与dp算法

    题目:数字三角形 题目介绍:如图所示的数字三角形,要求从最上方顶点开始一步一步下到最底层,每一步必须下一层,求出所经过的数字的最大和. 输入:第一行值n,代表n行数值:后面的n行数据代表每一行的数字. ...

随机推荐

  1. Ajax的那点事

    Ajax中什么是同步.异步? 同步:就是用户填写完信息之后,全部提交给服务器,等待服务器的回应,是一次性全部的. 异步:当用户填写完一条信息之后,这条信息会自动向服务器提交,然后服务器响应客户端,在此 ...

  2. [CF1202B] You Are Given a Decimal String(最短路)

    Description 今天突然想来发一篇博客防死 [Portal][https://vjudge.net/problem/2650668/origin] 定义被x-y生成器生成的序列为, 一开始有一 ...

  3. C++中函数调用操作符的重载

    1,本博文讲述函数对象问题: 2,客户需求: 1,编写一个函数: 1,函数可以获得斐波那契数列每项的值: 2,每调用一次返回一个值: 3,函数可根据需要重复使用: 4,代码示例: ; i<; i ...

  4. LVS _keepalived 配置

    #!/bin/bash HOSTNAME=$(HOSTNAME) ETHNAME=ens34 ID=-]{,}\.[-]{,}\.[-]{,}\.[-]{,}" | awk -F . 'NR ...

  5. Hadoop伪分布式环境安装

    一.环境准备 阿里云ECS(Centos7).已预装JDK8 Hadoop安装包 hadoop-2.7.7.tar.gz 二. 安装步骤 1.确认JDK环境的安装位置 命令 echo $JAVA_HO ...

  6. 轻量级Spring定时任务(Spring-task)

    Spring3.0以后自主开发的定时任务工具,spring-task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式. ...

  7. SSD的理解,为PyramidBox做准备

    目标检测主流方法有两大类 two-stage,以rcnn系列为主,采用建议框的方式对目标进行预测,过程首先要经过网络生成候选框,分类背景前景与进行第一次回归,之后再进行一次精细回归. 优点是准确率高, ...

  8. sparksql 自定义用户函数(UDF)

    自定义用户函数有两种方式,区别:是否使用强类型,参考demo:https://github.com/asker124143222/spark-demo 1.不使用强类型,继承UserDefinedAg ...

  9. js学习笔记-日期对象

    <body> <script> var d = new Date() console.log(d) var arr = ['星期日', '星期一', '星期二', '星期三', ...

  10. PHP closedir() 函数

    打开一个目录,读取它的内容,然后关闭: <?php$dir = "/images/"; // Open a directory, and read its contentsi ...