算法学习——递归, 以及基于递归的DFS(深度优先搜索)

一、最基础的递归问题

斐波那契数列问题

以下数列 0 1 1 2 3 5 8 13 21 ... 被称为斐波纳契数列。

这个数列从第3项开始,每一项都等于前两项之和。

输入一个整数N,请你输出这个序列的第N项。

若利用递归的方法来解决,思路如下。

相应代码

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

int n;

int f(int n){
    if(n == 1) return 0;
    
    if(n == 2) return 1;
    
    return f(n-1) + f(n-2);
}

int main(){
    scanf("%d", &n);
    printf("%d", f(n));
    return 0;
}

更多有关斐波那契数列问题的优化解法,具见文章https://blog.csdn.net/m0_72955669/article/details/135984470

二、常见的递归问题类型

通常情况下,枚举问题均可以用DFS来做。

思考工具:DFS搜索树

1.递归实现指数型枚举

(1)题目

从 1∼n 这 n个整数中随机选取任意多个,输出所有可能的选择方案。

输入格式

输入一个整数 n。

输出格式

每行输出一种方案。

同一行内的数必须升序排列,相邻两个数用恰好 1个空格隔开。

对于没有选任何数的方案,输出空行。

本题有自定义校验器(SPJ),各行(不同方案)之间的顺序任意。

数据范围

1≤n≤15

输入样例:

3

输出样例:


3
2
2 3
1
1 3
1 2
1 2 3

(2)思路及代码

思路1

利用数组进行记录,未枚举的地方标记为0,枚举到但没有选的地方标记为1,枚举到选择的地方标记为2。

相应代码

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N = 20;

int a[N];

void dfs(int index, int n){
    if(index > n){
        for(int i = 1; i <= n; ++ i){
            if(a[i] == 2) printf("%d ", i);
        }
        printf("\n");
        return;
    }
    
    a[index] = 1;
    dfs(index+1, n);
    a[index] = 0;
    
    a[index] = 2;
    dfs(index+1, n);
    a[index] = 0;
}

int main(){
    int n;
    scanf("%d", &n);
    dfs(1,n);
    return 0;
}

思路2

相应代码

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N = 20;

int a[N];

int n, step = 1;

void dfs(int step, int st){
    
    if(step > n) return;
    
    for(int i = st; i <= n; ++ i){
        a[step] = i;
        
        dfs(step+1, i+1);
        
        for(int j = 1; j <= step; ++ j){
            printf("%d ", a[j]);
        }
        printf("\n");
        a[step] = 0;
    }
}

int main(){
    scanf("%d", &n);
    printf("\n");
    dfs(1, 1);
    return 0;
}

2.递归实现排列型枚举

(1)题目

把 1∼n 这 n个整数排成一行后随机打乱顺序,输出所有可能的次序。

输入格式

一个整数 n。

输出格式

按照从小到大的顺序输出所有方案,每行1个。

首先,同一行相邻两个数用一个空格隔开。

其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面。

数据范围

1≤n≤9

输入样例:

3

输出样例:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

(2)思路及代码

思路1

思路2

思路1,2本质相同,因此实现代码相同。

相关代码

#include<iostream>
#include<cstdio>
#include<cstring>

const int N = 10;

int a[N];
int visited[N];

void dfs(int step, int n){
    if(step > n){
        for(int i = 1; i <= n; ++ i){
            printf("%d ", a[i]);
        }
        printf("\n");
    }
    
    for(int i = 1; i <= n; ++ i){
        if(!visited[i]){
            a[step] = i;
            visited[i] = 1;
            
            dfs(step+1, n);
            
            a[step] = 0;
            visited[i] = 0;
        }
    }
}

int main(){
    int n;
    scanf("%d", &n);
    dfs(1, n);
    return 0;
}

3.递归实现组合型枚举

(1)问题

从 1∼n 这 n 个整数中随机选出 m个,输出所有可能的选择方案。

输入格式

两个整数 n,m,在同一行用空格隔开。

输出格式

按照从小到大的顺序输出所有方案,每行1个。

首先,同一行内的数升序排列,相邻两个数用一个空格隔开。

其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面(例如 1 3 5 7 排在 1 3 6 8 前面)。

数据范围

n>0,0≤m≤n ,n+(n−m)≤25

输入样例:

5 3

输出样例:

1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 

(2)思路及代码

思路

时间复杂度为 O(n*n!)。

空间复杂度为 O(n)。

相关代码

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N = 30;

int a[N];
int n, m, step;

void dfs(int step, int st){
    
    if(n - st < m - step) return; // 剪枝
    
    if(step > m){
        for(int i = 1; i <= m; ++ i){
            printf("%d ", a[i]);
        }
        printf("\n");
        return;
    }
    
    for(int i = st; i <= n; ++ i){
        a[step] = i;
        
        dfs(step+1, i+1);
        
        a[step] = 0;
    }
}

int main(){
    scanf("%d%d", &n, &m);
    dfs(1, 1);
    return 0;
}

总结:解决递归问题通常可以利用思路1和思路2用到的两种不同的递归树。在解决枚举类型的问题时,思路1的递归搜索树更直观,因此用得更多。

三、经典递归问题

拓展题目

1 带分数

具见如下文章

https://blog.csdn.net/m0_72955669/article/details/135791277

2 飞行员兄弟

具见如下文章(方法3)

https://blog.csdn.net/m0_72955669/article/details/135995735

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值