搜素专题(DFS

前言

 搜素是一种暴力的方法可以按照树去理解在不剪支的情况下,可以把所有 *方案* “枚举”出来剪支 --> 在确定一定不会是解的情况下,提前终止该"点"下面的搜素

类似树结构进行


一个问题可以被拆分成几个子部分去解决(有点DP的味道了,但本节课只是简单的搜索,方向食用)

换成人话来说:

如果把问题抽象成集合,每一个元素都可以看成一个局部的方案

所有 由一个个元素构成的子集 都可能会是问题的一个解

如果还有点晕

来一个实际的问题助助兴吧

123 这三个数全排列

图上每一个集合,都是问题的一个解

如:3 1 2


全排列实现代码

#include <bits/stdc++.h>
#define buff                     \ios::sync_with_stdio(false); \cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
bool st[N];
int ans[N];
int n;
void dfs(int cnt)
{if (cnt == n){for (int i = 1; i <= n; i++){cout << ans[i];cout << (i == n ? '\n' : ' ');}}for (int i = 1; i <= n; i++){if (!st[i]){ans[cnt + 1] = i;st[i] = 1;dfs(cnt + 1);st[i] = 0;}}
}
void solve()
{cin >> n;for (int i = 1; i <= n; i++){ans[1] = i;st[i] = 1;dfs(1);st[i] = 0;}
}
int main()
{solve();
}

模板

void dfs(需要使用的参数)
{if(满足题意的条件){xxx}if(边界条件 | 剪支){xxxreturn }灵活的dfs过程根据题意书写,很灵活!怎么舒服怎么来 (:for (条件){xxxxxxst[idx] = 1;//****dfs(xxx)st[idx] = 0//****}ordfs(xxx)等等cout << ans << '\n';
}

题目

P2392 kkksc03考前临时抱佛脚

https://www.luogu.com.cn/problem/P2392

AC代码
#include <bits/stdc++.h>
#define buff                     \ios::sync_with_stdio(false); \cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;int a[N];
int n1, n2, n3, n4;
int ans = 0, tt = 0;
int nn;
void dfs(int l, int r, int cnt)
{if (cnt == nn){tt = min(tt,max(l, r));return;}dfs(l + a[cnt + 1], r, cnt + 1);dfs(l, r + a[cnt + 1], cnt + 1);
}
void solve()
{cin >> n1 >> n2 >> n3 >> n4;for (int i = 1; i <= n1; i++)cin >> a[i];nn = n1, tt = 0x3f3f3f3f;dfs(0, 0, 0);ans += tt;for (int i = 1; i <= n2; i++)cin >> a[i];nn = n2, tt = 0x3f3f3f3f;dfs(0, 0, 0);ans += tt;for (int i = 1; i <= n3; i++)cin >> a[i];nn = n3, tt = 0x3f3f3f3f;dfs(0, 0, 0);ans += tt;for (int i = 1; i <= n4; i++)cin >> a[i];nn = n4, tt = 0x3f3f3f3f;dfs(0, 0, 0);ans += tt;cout << ans << endl;
}
int main()
{solve();
}

P1135 奇怪的电梯

https://www.luogu.com.cn/problem/P1135

T代码

太过暴力,没有处理好重复的局部方案

换句话来说:剪支处理不到位


#include <bits/stdc++.h>
#define buff                     \ios::sync_with_stdio(false); \cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
int s[N];
int n, bg, ed;
int ans = 0x3f3f3f3f;void dfs(int idx, int cnt)
{if (idx <= 0 || cnt > n)return ;if (idx == ed){ans = min(ans, cnt);return ;}dfs(idx + s[idx],cnt + 1);dfs(idx - s[idx],cnt + 1);
}void solve()
{cin >> n >> bg >> ed;for (int i = 1; i <= n; i++)cin >> s[i];dfs(bg,0);cout << (ans == 0x3f3f3f3f ? -1 : ans) << '\n';
}
int main()
{solve();
}
AC代码
#include <bits/stdc++.h>
#define buff                     \ios::sync_with_stdio(false); \cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
int s[N];
int n, bg, ed;
int ans = 0x3f3f3f3f;
bool st[N];
void dfs(int idx, int cnt)
{if (idx <= 0 || cnt > n || st[idx] || idx > n || cnt >= ans)return ;if (idx == ed){ans = min(ans, cnt);return ;}st[idx] = 1;dfs(idx + s[idx],cnt + 1);dfs(idx - s[idx],cnt + 1);st[idx] = 0;
}void solve()
{cin >> n >> bg >> ed;for (int i = 1; i <= n; i++)cin >> s[i];dfs(bg,0);cout << (ans == 0x3f3f3f3f ? -1 : ans) << '\n';
}
int main()
{solve();
}

本题の总结

1.标记的正确使用
2.剪支的判断 & 优化
...

计数类

P1605 迷宫

https://www.luogu.com.cn/problem/P1605

AC代码

#include <bits/stdc++.h>
#define buff                     \ios::sync_with_stdio(false); \cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
int n, m, t;
int bgx, bgy, edx, edy;
int s[N][N];
bool st[N][N];
int xx[4] = {0, 1, -1, 0}, yy[4] = {1, 0, 0, -1};
int ans;
void dfs(int x, int y)
{if (x == edx && y == edy){ans++;return;}for (int i = 0; i < 4; i++){int tx = x + xx[i], ty = y + yy[i];if ((tx >= 1 && tx <= n) && (ty >= 1 && ty <= m) && !s[tx][ty] && !st[tx][ty]){st[x][y] = 1;dfs(tx, ty);st[x][y] = 0;}}
}
void solve()
{cin >> n >> m >> t;cin >> bgx >> bgy >> edx >> edy;for (int i = 1; i <= t; i++){int a, b;cin >> a >> b;s[a][b] = 1;}dfs(bgx, bgy);cout << ans << endl;
}
int main()
{solve();
}

搜素专题(DFS )相关推荐

  1. 老男孩博客获三大搜素引擎搜索自然排名第一位(百度谷歌搜狗)

    老男孩博客获百度.谷歌.搜狗,三大搜素引擎搜索自然排名第一位,可喜可贺! 感谢所有朋友,感谢51CTO,感谢各大搜索引擎的公平收录. 就是公众媒体对老男孩培训的最好肯定,也体现了老男孩培训的真正实力! ...

  2. LeetCode 73矩阵置零74搜素二维矩阵75颜色分类

    新人公众号(求支持):bigsai 专注于Java.数据结构与算法,一起进大厂不迷路! 算法文章题解全部收录在github仓库bigsai-algorithm,求star! 关注回复进群即可加入力扣打 ...

  3. C语言循环遍历文件夹查找文件内容(搜素/proc文件夹下的内容获取进程pid)

    参考文章:通过搜素/proc文件夹下的内容获取进程pid

  4. 记忆化搜素,和递推法

    记忆化搜素是动态规划的改进,------自上而下,就是在递归重叠子问题时候,对子问题的重复问题的对策,就是一开始对所有子问题进行赋值(一般为-1)这样的标记方法来区分是否被查找过. 递推----也是动 ...

  5. elasticsearch搜素关键字自动补全(suggest)

    elasticsearch搜素关键字自动补全顾名思义 在搜索框搜索时能有提示列表可供选择. 最终效果如下: 该搜索优化功能是elasticsearch自带的即suggest,suggest即存储一个词 ...

  6. android布局新建联系人,Android中设置搜素联系人的布局

    我们现在要达到下面的效果: 我们这样做了:我们可以先定义一个线性布局: 在线性布局中加入一个ImageView和一个Edittext 最后给这个线性布局加入背景,该背景就是外面的一个灰色边框. 我们来 ...

  7. Struts2中Action的搜素顺序

    当我们在struts.xml中配置action的时候,设置了package的namepace,但浏览器打开的路径与其不相同也能运行action. 比如:我们的创建一个struts2项目,项目名为:st ...

  8. 二叉树的深度(前序 中序 后序 递归非递归搜素)、广度、搜索 C++

    a b c 使用 1 2 3 表示 /* 描述:二叉树的深度(前序 中序 后序 递归非递归搜素).广度.搜索 作者:jz 日期:20140819 */ #include<stdio.h> ...

  9. Google 搜素技巧分享

    平时在工作中我们经常用到Google 来搜素一些工作中需要的信息,下面是我在网上看到的一篇文章给大家分享一下,希望对大家的工作有所帮助 转自网络: 大前提:英文Google→www.google.co ...

最新文章

  1. python将sklearn的RocCurveDisplay结果与PrecisionRecallDisplay结果合成为一个图
  2. 2021年AI将改变制造业的6大应用趋势
  3. c 语言程序设计教程 沈显君 答案,CD3计算机实践《C/C++语言程序设计》报告模板2.doc...
  4. 关于keil环境的 三个红点(备忘)
  5. 轻松实现突破网管限制(SoftEther实际应用)
  6. Entity Framework Core 6.0 预览4 性能改进
  7. 【AC自动机】前缀匹配(ybtoj AC自动机-3)
  8. python 系统时间24小时制_Python 日期和时间
  9. 【azkaban】学习azkaban的笔记以及心得
  10. Java实现Oracle导出数据到Excel
  11. C程序设计(第四版)谭浩强著-学习笔记
  12. Java接口自动化测试框架
  13. iphone邮件服务器 263,IPHONE中设置使用企业邮箱(以263为例).doc
  14. 计算机操作系统 共享性,计算机操作系统的功能和分类探析
  15. html如何修改title前的小图标
  16. 爱荷华大学计算机科学专业,2015 U.S News计算机科学专业排名(不知道有没有伙伴在找)...
  17. 【javafx】如何java查询12306火车票剩余数量
  18. ccsa安学网小程序_CCSA安学网题库1
  19. 数学图形(1.4)心形线
  20. VS粘贴word时中文乱码修复工具v1

热门文章

  1. 360网站卫士的IP段添加进服务器的白名单中
  2. editplus中文乱码问题解决
  3. 【Pyecharts50例】日历图/自定义日历图样式/CalendarOpts
  4. 单纯的一个复杂的json例子
  5. IBM ServeRAID Manager 9.30
  6. 行人重识别AlignedReID:AlignedReID: Surpassing Human-Level Performance in Person Re-Identificat 重点亮点学习资料整理
  7. Cannot access memory at adress 0xbf9
  8. 电视剧《天道》里的商业思维
  9. 英式音标26字母发音规律
  10. HDLBits 状态机练习题目 water reservoir 蓄水池控制器