算法基础篇(10)递归型枚举与回溯剪枝

搜索,是一种枚举,通过穷举所有情况来找到最优解或者统计合法解的个数。搜索一般分为深度优先搜索 (DFS)与 宽度优先搜索(BFS)

回溯:在搜索过程中,遇到走不通或者走到底的情况,就回头

剪枝:剪掉在搜索过程中重复出现或者不是最优解的分支

1、枚举子集

cpp 复制代码
#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

int n;
string path; //记录递归过程中,每一步的决策

void dfs(int pos)
{
	if (pos > n)
	{
		//path存着前n个人的决策
		cout << path << endl;
		return;
	}

	//不选
	path += "N";
	dfs(pos + 1);
	path.pop_back(); //回溯,清空现场

	//选
	path += "Y";
	dfs(pos + 1);
	path.pop_back();
}

int main()
{
	cin >> n;

	dfs(1);

	return 0;
}

1.2 组合型枚举

cpp 复制代码
#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
using namespace std;

int n, m;
vector<int> path;

void dfs(int begin)
{
	if (path.size() == m)
	{
		for (auto e : path)
			cout << e << " ";

		cout << endl;
		return;
	}

	for (int i = begin;i <= n;i++)
	{
		path.push_back(i);
		dfs(i + 1);
		path.pop_back();
	}
}

int main()
{
	cin >> n >> m;

	dfs(1);

	return 0;
}

1.3 枚举排列

cpp 复制代码
#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
using namespace std;

int n, k;
vector<int> path;

const int N = 20;
bool st[N];

void dfs()
{
	if (path.size() == k)
	{
		for (auto e : path)
			cout << e << " ";

		cout << endl;
		return;
	}

	for (int i = 1;i <= n;i++)
	{
		if (st[i])
			continue;
		
		path.push_back(i);
		st[i] = true;					
		dfs();

		//恢复现场
		st[i] = false;
		path.pop_back();
	}
}

int main()
{
	cin >> n >> k;

	dfs();

	return 0;
}

1.4 全排列

cpp 复制代码
#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

int n;
vector<int> path;

const int N = 10;
bool st[N];

void dfs()
{
	if (path.size() == n)
	{
		for (auto e : path)
		{
			printf("%5d", e);
		}
		cout << endl;
		return;
	}
		
	for (int i = 1;i <= n;i++)
	{
		if (st[i])
			continue;

		path.push_back(i);
		st[i] = true;
		dfs();

		path.pop_back();
		st[i] = false;
	}
	
}

int main()
{
	cin >> n;

	dfs();

	return 0;
}
相关推荐
setmoon2145 分钟前
多协议网络库设计
开发语言·c++·算法
Sylvia-girl7 分钟前
删除有序数组中的重复项
数据结构·算法
2501_908329858 分钟前
嵌入式LinuxC++开发
开发语言·c++·算法
Storynone8 分钟前
【Day30】卡码网:46. 携带研究材料,LeetCode:416. 分割等和子集
python·算法·leetcode
少许极端10 分钟前
算法奇妙屋(三十四)-贪心算法学习之路 1
学习·算法·贪心算法
兑生12 分钟前
【灵神题单·贪心】3010. 将数组分成最小总代价的子数组 I | Java
java·开发语言·算法
垫脚摸太阳20 分钟前
二分查找经典算法题--数的范围
数据结构·算法
噜啦噜啦嘞好20 分钟前
算法篇:二分查找
数据结构·c++·算法·leetcode
setmoon21420 分钟前
C++中的构建器模式
开发语言·c++·算法
2301_8154829320 分钟前
C++中的桥接模式变体
开发语言·c++·算法