首页 > 其他分享 >DFS

DFS

时间:2024-04-13 15:13:11浏览次数:17  
标签:arr 20 int namespace dfs DFS

DFS

DFS指数型枚举模板

image-20240412122753272

bbd1e15879cc804559cc6083bf2e141

#include<iostream>
using namespace std;

int arr[20];
int n;

void dfs(int x){
    if (x > n ){  // 出口 DFS位置大于选择的n
        
        for (int i = 1; i <=n ; ++i){
            if (arr[i] == 1){   // 如果标志为1 的 被选中的打印出
                cout << i << " ";
            }
    
        }
         cout << endl;
         return;
    }
    
    arr[x] = 1; // 选中
    dfs(x + 1); // DFS
    arr[x] = 0; // 回溯
    
    arr[x] = 2; // 不选中
    dfs(x + 1);  // DFS
    arr[x] = 0; // 回溯
}


int main(){
    cin >> n;
    dfs(1);
    
}

全排列

image-20240412124922322

微信图片_20240412131939

#include<bits/stdc++.h>
using namespace std;

int n;
bool st[20];
int arr[20];

void dfs(int x){
    // 出口条件
	if (x > n){
		for (int i = 1;i <= n ; ++i){
			cout <<"    "<< arr[i];
		}
		cout << endl;
		return;
	}
    
    // 三个分支
	for (int i = 1; i<= n; ++i){
        
		if (!st[i]){ // 如果这个数没被选中
			st[i] = true; // 上锁
			arr[x] = i; // 给予这个数
			dfs(x+1); // DFS
			st[i] = false; // 回溯
			arr[x] = 0;
		}
	}
}
int main(){
	cin >> n;
	dfs(1);
} 

DFS组合枚举

image-20240412132508989

#include<bits/stdc++.h>
using namespace std;
int n, r;
int arr[20];

void dfs(int x, int start){
	
	if (x > r){
		for ( int i = 1; i <= r ; ++i){
			cout << setw(3) << arr[i];
		}
		cout << endl;
		return;
	}
	
	for (int i = start ; i <= n ;++i ){
		arr[x] = i;
		dfs(x+1,i+1);
		arr[x] = 0;
	}
}

int main(){
	cin >> n >> r; 
	dfs(1,1);
	return 0;
} 

标签:arr,20,int,namespace,dfs,DFS
From: https://www.cnblogs.com/wbcde116/p/18132871

相关文章

  • dfs序专题训练
    DFS序专题NC13611https://ac.nowcoder.com/acm/problem/13611题意:要求树上任意两点相同颜色之间的路径上的点也是相同颜色,k种颜色,求方案数Solution:原问题等价于将树分割成若干连通块且相互之间颜色不同其实是道数论题。题意可以转化为将树分割为不超过\(k\)个连通块,每个连......
  • P9669 [ICPC2022 Jinan R] DFS Order 2
    P9669[ICPC2022JinanR]DFSOrder2树形dp+回退背包dfs的过程时走到\(u\),如果走进一个子树后要回到\(u\),那么这个子树一定全部遍历了一遍。所以方案数会跟子树遍历的方案数有关,可以预处理。设\(h_u\)表示\(u\)子树的遍历方案,假如\(u\)有\(m\)个儿子,那么有\(h_u=......
  • 买瓜-dfs
    6.买瓜-蓝桥云课(lanqiao.cn)#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;intn,m,a[35],cnt=40,sum[35];//x是当前编号,w是当前总重量,c是当前劈了多少个瓜voiddfs(intx,intw,intc){ if(w==m){//如果当前重量符合要......
  • Acwing2024蓝桥杯DFS
    模板:AcWing826.单链表利用数组创建单链表:#include<iostream>usingnamespacestd;constintN=100005;inthead,value[N],nextt[N],id;voidInit(){head=-1,id=0;}voidHead_Insert_x(intx){value[id]=x;nextt[id]=head;head=id++;}voidD......
  • HDFS报错:Couldn‘t preview the file.
    packagecom.qm.hdfs;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.FileSystem;importorg.apache.hadoop.fs.Path;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importjava.io.IOException;importjava.n......
  • Java实现Fast DFS、服务器、OSS上传
    支持FastDFS、服务器、OSS等上传方式介绍在实际的业务中,可以根据客户的需求设置不同的文件上传需求,支持普通服务器上传+分布式上传(FastDFS)+云服务上传OSS(OSS)软件架构为了方便演示使用,本项目使用的是前后端不分离的架构前端:Jquery.uploadFile后端:SpringBoot前期准备:F......
  • Acwing 681. 疏散人群(dfs)(记录根节点下有几个子节点)
    输入样例:62132435261输出样例:4#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;typedefpair<LL,LL>PII;constLLN=100200,M=2020;constLLmod=998244353;vector<LL>g[N];LLsum[N];LLdfs(LLidx,LLfa){LL......
  • 代码随想录 | 图论 797. 所有可能的路径(dfs) ,200. 岛屿数量 (dfs)200. 岛屿数量 (bfs)
    797.所有可能的路径https://leetcode.cn/problems/all-paths-from-source-to-target/description/List<List<Integer>>res;List<Integer>path;publicList<List<Integer>>allPathsSourceTarget(int[][]graph){res=newA......
  • 4. 飞机降落-dfs
    4.飞机降落-蓝桥云课(lanqiao.cn)230100101010100220301020101020201020#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;structplane{intt,d,l;}p[12];intn;//pan数组用来判断当前飞机降落了么......
  • 蓝桥杯,省赛,dfs专题,地宫取宝,小朋友崇拜圈,飞机降落
    #include<bits/stdc++.h>usingnamespacestd;intn,m,k;inta[55][55];//输入所给数组值所分配的内存空间intdp[55][55][15][15];//开创记忆化的存储空间//因为只进行向下走和向右走,所有写成这个样子,不明白的可以在了解以下笛卡尔积,向下是x轴,向右是y轴(一般情况下)int......