网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
【DFS专题】优质题单推荐 10道题(C++ | 洛谷 | acwing)
题单
一、模板 [极为重要]
全排列DFS
- 每个位置选什么数
#include<iostream>
using namespace std;
const int N = 10;
int path[N];//保存序列
int state[N];//数字是否被用过
int n;
void dfs(int u)
{
if(u > n)//数字填完了,输出
{
for(int i = 1; i <= n; i++)//输出方案
cout << path[i] << " ";
cout << endl;
}
for(int i = 1; i <= n; i++)//空位上可以选择的数字为:1 ~ n
{
if(!state[i])//如果数字 i 没有被用过
{
path[u] = i;//放入空位
state[i] = 1;//数字被用,修改状态
dfs(u + 1);//填下一个位
state[i] = 0;//回溯,取出 i
}
}
}
int main() {
cin >> n;
dfs(1);
}
组合型DFS
- 与全排列的差别就是第二个for循环开始的位置,换句话说就是每个数该放什么位置。
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
int path[N];
int n, m;
void dfs (int u, int start ) {//u:层数 start:起始的数值
if (u > m) {
for (int i = 1; i <= m; i ++ ) {
cout << path[i] << ' ';
}
puts("");
}
else {
for (int i = start; i <= n; i ++) {//
path[u] = i;//表示已经填了
dfs(u + 1, i + 1);//递归下一层
path[u] = 0;//恢复现场
}
}
}
int main () {
cin >> n >> m;
dfs(1,1); //第一层开始 且从1开始枚举
return 0;
}
指数DFS
- 参数 : 前u个数 选 or 不选 的
- 需要保存第x位置的状态的时候就需要用st数组来存状态
- int st[] 0:没有遇见 1 选 2不选
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 16;
int st[N];
int n;
void dfs (int u) {//u :层数
if (u > n) {//叶子结点
for (int i = 1; i <=n; i ++ ){
if (st[i] == 1) {//如果选了 就输出 1选 2不选
cout << i << ' ';
}
}
puts("");
return ;
}
st [u] = 1;//选
dfs (u + 1);//递归下一层
st[u] = 0;//回溯
st[u] = 2;//不选
dfs (u+1);//递归下一层
st[u] = 0;//回溯 【恢复现场】
}
int main () {
cin >> n;
dfs(1);
return 0;
}
二、专题
烤鸡 (指数BFS)
- 每个方案有3种选择,枚举全部则有 310 种方案数
https://www.luogu.com.cn/problem/P2089
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20;
int n;
int arr[N]; // 存储临时答案
int res = 0;// 方案数量
int mem[60000][N]; // 存储总方案数
// 60000 >= 3^10 (最多枚举数量)
// x : 当前枚举到哪个数 sum : 当前总和
void dfs(int x, int sum ) {
if(sum > n) return ;// 剪枝
if(x > 10) {
if(sum == n) {
res ++;
for(int i = 1; i <= 10; i ++) {
mem[res][i] = arr[i];
}
}
return;
}
for(int i = 1; i <= 3; i ++) {
arr[x] = i;
dfs(x + 1, sum + i);
arr[x] = 0; // 恢复现场
}
}
int main () {
cin >> n;
dfs(1, 0);
printf("%d\n" , res);
for (int i = 1; i <= res; i ++ ) {
for(int j = 1; j <= 10; j ++) {
printf("%d ", mem[i][j]);
}
printf("\n");
}
return 0;
}
P1088 火星人 【全排列】
- https://www.luogu.com.cn/problem/P1088
- 为什么要 m + 1
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, m;
int res = 0;
int ans[N], a[N];
bool st[N];
bool flag = false;
void dfs(int x) {
if(flag) return; //剪枝 因为只会输出一次结果
if(x > n) {
res ++;
if(res == m + 1) {
for(int i = 1; i <= n; i ++ ){
printf("%d ", ans[i]);
}
flag =true;
}
return ;
}
for (int i = 1; i <= n; i ++ ){
if(!res) {
i = a[x]; // 起点
}
if(! st[i]) {
st[i] = true;
ans[x] = i;
dfs(x + 1);
ans[x] = 0;
st[i] = false;
}
}
}
int main () {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);;
dfs(1);
return 0;
}
P1149 火彩棒 [预处理 ]
https://www.luogu.com.cn/problem/P1149
- 思路一 、 模拟
- 思路二 :预处理 + dfs 枚举
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n;
int res = 0;
int s[N];
int num[N] = {6,2,5,5,4,5,6,3,7,6};
void dfs(int x, int sum) {
if(sum > n ) return ;
if(x > 3) {
if(s[1] + s[2] == s[3] && sum == n) {
res ++;
}
return ;
}
//枚举前 1000
for(int i = 0; i <= 1000; i ++) {
s[x] = i;
dfs(x + 1, sum + num[i]) ;
s[x] = 0;
}
}
int main () {
scanf("%d", &n);
n -= 4;
//递推求前1000个数
for (int i = 10; i <= 1000; i ++ ) {
num[i] = num[i % 10] + num[i / 10];
}
dfs(1, 0);
printf("%d\n" , res);
return 0;
}
P2036 PERKET
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
![img](https://img-blog.csdnimg.cn/img_convert/83ecfbf6b7f4d857f4958b92cedd9fd4.png)
![img](https://img-blog.csdnimg.cn/img_convert/192cdc6217e147d547ac3e1298a3e9ca.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**
#include
#include
#include
using namespace std;
[外链图片转存中…(img-AdrPrKGY-1715590333685)]
[外链图片转存中…(img-yreEI4lG-1715590333686)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新