递归
迷麟Milin
There isn't time, so brief is life, for bickerings, apologies, heartburnings, callings to account. There is only time for loving, and but an instant, so to speak, for that. The good life is built with good relationships.
——Mark Twain
展开
-
汉诺塔问题——递归(时隔9个月,终于懂了)
记得我第一次做汉诺塔这道题时,是2017年11月。当时,我坐在山大青岛校区图书馆3楼,不知怎么地,看到了这个题。然后,就思考了一整天,233当然,悲剧就是,我当时花了一天的时间还是没有真正理解这道题递归的思路。如今,我终于懂了,嘿嘿嘿。关于递归: 一定不要试图跟踪大型递归的过程! 要写出递归,关键就是找出递归的递归方程式: 也就是说,要完成最后一步,那么最后一步的前一步要做什...原创 2018-08-24 21:18:03 · 60538 阅读 · 41 评论 -
DFS模板 洛谷 P2089 P1605 P1101
DFS模板int search(int t){ if(满足输出条件) { 输出解; } else { for(int i=1;i<=尝试方法数;i++) if(满足进一步搜索条件) { 为进一步搜索所需要的状态打上标记; ...原创 2020-02-04 18:13:59 · 328 阅读 · 0 评论 -
BFS、DFS复杂度分析(时间、空间)
BFS的复杂度分析。 BFS是一种借用队列来存储的过程,分层查找,优先考虑距离出发点近的点。无论是在邻接表还是邻接矩阵中存储,都需要借助一个辅助队列,v个顶点均需入队,最坏的情况下,空间复杂度为O(v)。邻接表形式存储时,每个顶点均需搜索一次,时间复杂度T1=O(v),从一个顶点开始搜索时,开始搜索,访问未被访问过的节点。最坏的情况下,每个顶点至少访问一次,每条边至少访问1次,这是因为在搜索的过程中,若某结点向下搜索时,其子结点都访问过了,这时候就会回退,故时间复 杂度为O(E),算法总的时间复 度为转载 2020-07-15 21:20:58 · 21642 阅读 · 0 评论 -
王道机试 第9章 深度优先搜索 DFS
王道机试 第9章 搜索深度优先搜索 DFS深度优先搜索模板void dfs(int cur){ if (cur == n && 其他到达终点的条件) { 输出答案; } if (剪枝) return; else for (int i = 0; i < n; i++) //遍历n种情况 { if (其他可达条件 && !vis[i...原创 2020-05-06 00:06:41 · 286 阅读 · 0 评论 -
PAT The Largest Generation
题目描述:A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population....原创 2020-01-27 10:52:01 · 128 阅读 · 0 评论 -
1087 All Roads Lead to Rome
题目大意:单源最短路,利用Dijkstra输出最短路径,当最短路有多条时按照给定原则输出优先级最高者的相关信息及其路径。本题与PAT:1003 Emergency 的差别主要为:1. 顶点不是int型而是给出string型,需要利用map实现顶点string名称和顶点int序号的映射,即:map<string, int> cityid;ma...原创 2019-11-02 22:00:33 · 129 阅读 · 0 评论 -
编写一个递归模板函数,确定元素x是否属于数组a[0:n-1]
利用pos记录递归的层数,当递归到第n + 1层(也就是到了判断x == a[n]时)返回false C++代码如下:#include <bits/stdc++.h> // 万能头文件(注意:POJ上无法使用)using namespace std;template<class T>bool in_the_array(const vector<T...原创 2018-09-06 15:47:00 · 2312 阅读 · 0 评论