当前位置:网站首页>DFS详解
DFS详解
2022-08-02 00:11:00 【真的没事鸭】
所谓的DFS就是深度优先遍历,一条路走到黑,走到无路可走了才会选择回头,
DFS俗称爆搜,深搜,最重要的就是我们按照什么顺序来把题目全部遍历一下。DFS对应的流程是一个树的结构,DFS的精髓在于递归求解的思路以及回溯的处理。针对搜索的过程,又有重要的剪枝优化。必要的剪枝优化对DFS的顺序执行有很大的作用。
DFS的过程就是沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都搜过,搜索回溯到发现节点v的那条边的起始节点。
DFS使用的数据结构是栈,时间复杂度是O(n),DFS不具有最短性,也就是DFS搜到的路径不一定是最短路。
DFS的模板框架
function dfs(当前状态){
if(当前状态 == 目的状态){
···
}
for(···寻找新状态){
if(状态合法){
vis[访问该点];
dfs(新状态);
?是否需要恢复现场->vis[恢复访问]
}
}
if(找不到新状态){
···
}
}
剪枝优化
剪枝是在不跳过最优解的情况下,采取必要的手段跳过不含有最优解的分支,减少搜索的次数,减少程序运行的时间。
剪枝优化这里就不赘述了。
DFS经典案例
给定一个整数 nn,将数字 1∼n 排成一排,将会有很多种排列方法。
现在,请你按照字典序将所有的排列方法输出。
输入格式
共一行,包含一个整数 n。
输出格式
按字典序输出所有排列方案,每个方案占一行。
数据范围
1≤n≤7
输入样例:
3
输出样例:
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
思路
利用DFS一层一层搜,搜完了就回溯,然后再根据结束条件判断是否搜索完毕
![]()
代码实现
#include <iostream>
using namespace std;
const int N=10;
int p[N];
int n;
bool st[N];
void dfs(int u)
{
//结束条件
if(u>n)
{
for(int i=1;i<=n;i++)
cout<<p[i]<<" ";
cout<<endl;
return;
}
//寻找新节点
for(int i=1;i<=n;i++)
{
//判断是否合法
if(!st[i])
{
p[u]=i;
st[i]=true;
dfs(u+1);
st[i]=false;
}
}
}
int main()
{
cin>>n;
dfs(1);
return 0;
}
边栏推荐
- Short video seo search optimization main content
- 146. LRU cache
- Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
- Collection of NFT tools
- 如何发现新的潜力项目?工具推荐
- GIF making - very simple one-click animation tool
- After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
- 重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
- Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
- 业务测试如何避免漏测 ?
猜你喜欢
短视频SEO优化教程 自媒体SEO优化技巧方法
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
路由策略
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
接地气讲解TCP协议和网络程序设计
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
Quick solution for infix to suffix and prefix expressions
协作乐高 All In One:DAO工具大全
短视频seo搜索优化主要内容
零基础如何学习单片机,一位入门者的进阶路径,可参考
随机推荐
Multi-feature fusion face detection based on attention mechanism
单片机遥控开关系统设计(结构原理、电路、程序)
CRS management and maintenance
CRS 管理与维护
Unknown CMake command "add_action_files"
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
基于数据驱动的变电站巡检机器人自抗扰控制
nodeJs--各种路径
What is the function of the JSP out.println() method?
Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
146. LRU 缓存
在不完全恢复、控制文件被创建或还原后,必须使用 RESETLOGS 打开数据库,解释 RESETLOGS.
[21-Day Learning Challenge] A small summary of sequential search and binary search
Async/await principle and execution sequence analysis
bgp 聚合 反射器 联邦实验
Difference between JSP out.print() and out.write() methods
什么是低代码(Low-Code)?低代码适用于哪些场景?
JSP out. The write () method has what function?
146. LRU cache
Are test points the same as test cases?