目录
---------------递归部分-----------------
---------------递推部分-----------------
引子:递归求斐波那契数列
#include <iostream>
#include <algorithm>
using namespace std;
int f(int n)
{
if(n == 1) return 1;
if(n == 2) return 1;
return f(n - 1) + f(n - 2);
}
int main()
{
int n;
cin >> n;
cout << f(n) << endl;
return 0;
}
--------------------------------------------------递归部分--------------------------------------------------------
一、递归实现指数型枚举
题目分析
(1)什么是指数型枚举?
每个数有【选与不选】两种情况,即:共有2^n种枚举方案;
(2)该题在考什么算法?
————分析题目可知,我们大致看来,每个数字被选与否会影响方案的不同,所以,不选与被选的相互影响可以看作是递归问题,用dfs算法来求解。
所以,这是个递归问题,而所有的递归问题都可以转换成一棵 【 递归搜索树 】的dfs问题。
大致思路:从1~n,依次判断决定 每个数选还是不选。
在该题中,输入n后,可选范围即为:[1 ,n],那么想要求得输出 所有的可选方案,就需要针对于每一个数字【被选/不被选】的状态来求
那么这样一棵递归搜索树,怎么转换成代码呢?
——————通过这棵树,我们发现,我们采用dfs深搜算法,重点在于传递的参数,即:dfs(参数...)
因为输入n后,我们的可选范围[1,n]是知道的,我们需要知道 当前考虑到 第某个数 了,所以需要
参数1:当前考虑第几个数字,用u来表示 【小技巧:u不常用,不会与接下来我们要编写代码的变量冲突。所以暂且用来表示我们的 当前考虑的第几个数】
除此之外,我需要用一个布尔类型( bool )的数组state[ ]来记录当前考虑的数是否被选
参数2:bool 类型的state[ N ]数组来记录第u个数字是否被选
【技巧:我们将变量定义成全局变量,就可以省去在函数中传参的麻烦。】
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 16;
int n;
int state[N];//表示状态,记录是否该数字被选 0表示位置还没考虑,1表示选,2表示不选
void dfs(int u)
{
if(u > n)//边界 u到达n了,表示到达叶子节点,需要打印输出了
{
for(int i = 1;i <= n;i ++)
if(state[i] == 1)
cout << i << ' ';
cout << endl;
return;
}
state[u] = 2;//选择当前数的分支
dfs(u + 1);
state[u] = 0;//恢复现场
state[u] = 1;//不选当前数的分支
dfs(u + 1);
state[u] = 0;//恢复现场
}
int main()
{
cin>>n;
dfs(1);
return 0;
}
二、递归实现排列型枚举
题目分析
什么是排列型枚举?
——【位置】和【数字】之间是有顺序的。eg:123和132是不同的方案
字典序最小,只需要在遍历的时候,从小到大枚举即可,就能使得枚举方案满足字典序从小到大
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10;
int n;
int state[N];//当前位置是填的哪个数 0表示还未放数,1~n表示放了哪个数
bool used[N];//当前位置是否被占用 false表示没有被占用,true表示被占用
void dfs(int u)
{
if(u > n)//放满了
{
for(int i = 1;i <= n;i++) printf("%d ",state[i]);
puts("");
return;
}
for(int i = 1;i <= n;i++)
{
if(!used[i])
{
state[u] = i;
used[i] = true;
dfs(u + 1);
//恢复现场
state[u] = 0;
used[i] = false;
}
}
}
int main()
{
cin >> n;
dfs(1);
return 0;
}
三、递归实现组合型枚举
什么是组合型枚举?和排列型枚举代码上有何联系?如何去由排列型枚举转变成组合型枚举呢?
————不考虑顺序的枚举,是组合型枚举,eg:123和213是一种方案;而在排列型枚举中是属于不同方案
————代码稍后观察
————限制后面位置要放的数字比前一个位置要放的数字大,就可以满足不重复,实现了去重
普通写法
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int path[30];//路径,记录了每个位置采用的数字
int n, m;
void dfs(int u, int start)//u表示枚举到了哪个位置 ,start表示当前最小可以从哪个数开始枚举
{
if (u > m)
{
for (int i = 1; i <= m; i++)
printf("%d ", path[i]);
puts("");
return;
}
for (int i = start; i <= n; i++)
{
path[u] = i;
dfs(u + 1, i + 1);
path[u] = 0;//恢复现场
}
}
int main()
{
scanf("%d %d", &n, &m);
dfs(1, 1);
return 0;
}
剪枝优化写法
通过对比【普通写法】的运行时间143ms,我们想到了剪枝,即:如果这个方案不存在,就不继续递归下去了,及时退出
优化后的代码运行时间:40ms
优化方法的分析:
由于我们发现,eg:当4放在第一个位置上时,后面俩位置的数只能比4大,而现在只剩下一个数5,是【凑不够三个位置】的,那么就可以提前退出递归,节省时间。
如何剪枝优化?
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int path[30];//路径,记录了每个位置采用的数字
int n, m;
void dfs(int u, int start)//u表示枚举到了哪个位置 ,start表示当前最小可以从哪个数开始枚举
{
if(u + n - start < m) return;
if (u > m)
{
for (int i = 1; i <= m; i++)
printf("%d ", path[i]);
puts("");
return;
}
for (int i = start; i <= n; i++)
{
path[u] = i;
dfs(u + 1, i + 1);
path[u] = 0;//恢复现场
}
}
int main()
{
scanf("%d %d", &n, &m);
dfs(1, 1);
return 0;
}
----------------------------------------------递推部分--------------------------------------------------------
什么是递推?和递归有什么区别?
——————思维的“ 逆 ”
一、斐波那契数列递推写法
开数组写法:
#include<iostream>
//递推问题
using namespace std;
int main()
{
int n;
cin >> n;
int f[46];
f[1] = 0, f[2] = 1;
for(int i = 3;i <= n; i ++) f[i] = f[i - 1] + f[i - 2];
for(int i = 1; i<= n; i ++) cout << f[i] << ' ';
return 0;
}
不开数组,开变量
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 50;
int n;
int a, b, c;
int main()
{
cin >> n;
a = 0, b = 1;
for (int i = 0; i < n; i++) printf("%d ",a), c = a + b,a = b,b = c;
return 0;
}
二、费解的开关
思路
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 6;
int dx[N] = {-1, 0, 1, 0, 0}, dy[N] = {0, 1, 0, -1, 0};
char g[N][N], backup[N][N];
// 这个操作是把(x, y)以及上下左右的灯都变成相反的颜色
void turn (int x, int y)
{
for (int i = 0; i < 5; i ++ )
{
int a = x + dx[i], b = y + dy[i];
//如果在边界外边,直接忽略即可
if (a < 0 || a >= 5 || b < 0 || b >= 5) continue;
g[a][b] ^= 1; //异或,不同的时候就变成相反的数
}
}
int main()
{
int n;
scanf("%d", &n);
while(n -- )
{
// 按行输入,把每一行当成一个字符串
for (int i = 0; i < 5; i ++ ) cin >> g[i];
int res = 10;
// 这里我们枚举了第一行的32种按法,不用管是亮是灭,把第一行所有情况都按一遍
// 按每种情况的第一行,去遍历接下来的行
// 枚举32种第一行的按法只是可能会减少步数,如果直接从第二行开始答案一定是固定的了,找不到最优解或者可能没有解
for (int op = 0; op < 32; op ++ )
{
// 我在对这种情况操作的时候,得先备用一下
// 把原始数组备份一下,然后操作g,操作完了还原,然后再操作
memcpy(backup, g, sizeof g);
int step = 0;
// 第一行的按法(在这里 1 表示按了, 0 表示不按),这里只是为了输出第一行按完之后的状态
for (int i = 0; i < 5; i ++ )
if (op >> i & 1) // 数字2 对应了 00010 表示第2个位置的按一下
// 00010 >> 1 & 1 是1 所以turn(0, 1) 就是第一行第二个位置
{ // 数字3 对应了00011 表示第1 和第2个位置的按一下
step ++ ;
turn (0, i);;
}
// 然后通过第一行按完之后的状态,按234行
for (int i =0; i < 4; i ++ )
for (int j = 0; j < 5;j ++ )
if (g[i][j] == '0')
{
step ++;
turn (i + 1, j); // 如果这个位置是灭的,就按下一行对应的位置
}
bool dark = false;//看最后一行是不是还存在灭的情况
for (int j = 0; j < 5; j ++ )
if (g[4][j] == '0')
{
dark = true;
break;
}
// 对于32种情况的这一种,如果所有的全亮就记录下步数(事实上只记录了最后一行是否dark)
if (!dark) res = min(res, step);
memcpy (g, backup, sizeof g);
}
if(res > 6) res = -1;
cout << res << endl;
}
return 0;
}
下一讲预告,递归大量刷题,并学习【DP动态规划】问题