声明

本文不介绍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. webpack请求文件路径代理

    在开发模式下,访问页面请求会跑到根路径,因为写的都  ./images  而index又在根目录, 所以访问地址会是 http://localhost:8080/images/abc.jpg  而实际 ...

  2. ! Failed at the chromedriver@2.35.0 install script.

    npm install 过程中报错 解决方法 运行 npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/d ...

  3. MSDN上关于WinDbg的手册

    参考:http://msdn.microsoft.com/en-us/library/windows/hardware/ff540507(v=vs.85).aspx 这是最靠谱的参考了,比.hh要直观 ...

  4. ubuntu snap 包管理器

    基本使用 snap find $package snap install $package [--channel=beta] snap list snap refresh $package [--ch ...

  5. flink-training-course

    目录 flink-training-course 大数据领域顶级盛会 Flink Forward Asia 2019 详情

  6. C++中的多重继承(二)

    1,本文分析另一个多重继承问题及其工程中的解决方案,单继承加多接口实现的开发方式: 2,多重继承的问题三: 1,多重继承可能产生多个虚函数表: 1,实际工程中可能造成不可思议的问题,并且这些问题很难以 ...

  7. A - Max Sum Plus Plus (好题&&dp)

    A - Max Sum Plus Plus  I think you have got an AC in Ignatius.L's "Max Sum" problem. To be ...

  8. 39.Binary Tree Inorder Traversal(二叉树中序遍历)

    Level:   Medium 题目描述: Given a binary tree, return the inorder traversal of its nodes' values. 思路分析: ...

  9. go语言从例子开始之Example25.通道方向

    当使用通道作为函数的参数时,你可以指定这个通道是不是只用来发送或者接收值.这个特性提升了程序的类型安全性. Example: package main import "fmt" / ...

  10. Codeforces 353E 贪心

    题意:给你一张有向图,第i条边连接i号点和(i + 1) % n号点,问最多可以选择多少个点,使得这些点互相不可达. 思路:容易发现,如果某个边的集合点的数目大于等于2,那么就可以选出一个点,当然也可 ...