当前位置:   article > 正文

DFS与BFS算法总结_dfs bfs flord等算法对比

dfs bfs flord等算法对比

知识概览

  • DFS、BFS都可以对整个问题空间进行搜索,搜索的结构都是像一棵树。
  • DFS会尽可能往深搜,当搜索到叶节点时就会回溯。而BFS每一次只会扩展一层。

DFS与BFS的区别:

搜索方式数据结构空间复杂度性质
DFSO(h),其中h为搜索空间树的高度不具有最短性
BFS队列O(2^h),其中h为搜索空间树的高度有“最短路”概念

一、DFS算法(暴搜、深搜)

DFS的回溯

例题展示
题目链接

活动 - AcWing系统讲解常用算法与数据结构,给出相应代码模板,并会布置、讲解相应的基础算法题目。icon-default.png?t=N7T8https://www.acwing.com/problem/content/844/

题解

搜索顺序可以看每个位置填哪些数,属于全排列问题。

代码
  1. #include <iostream>
  2. using namespace std;
  3. const int N = 10;
  4. int n;
  5. int path[N];
  6. bool st[N];
  7. void dfs(int u)
  8. {
  9. if (u == n)
  10. {
  11. for (int i = 0; i < n; i++) printf("%d ", path[i]);
  12. puts("");
  13. return;
  14. }
  15. for (int i = 1; i <= n; i++)
  16. if (!st[i])
  17. {
  18. path[u] = i;
  19. st[i] = true;
  20. dfs(u + 1);
  21. st[i] = false;
  22. }
  23. }
  24. int main()
  25. {
  26. cin >> n;
  27. dfs(0);
  28. return 0;
  29. }

DFS的剪枝

例题展示
题目链接

活动 - AcWing系统讲解常用算法与数据结构,给出相应代码模板,并会布置、讲解相应的基础算法题目。icon-default.png?t=N7T8https://www.acwing.com/problem/content/845/

题解一

每一行只能有一个皇后,类似于全排列问题。时间复杂度为O(n\cdot n!)

代码一
  1. #include <iostream>
  2. using namespace std;
  3. const int N = 20;
  4. int n;
  5. char g[N][N];
  6. bool col[N], dg[N], udg[N];
  7. void dfs(int u)
  8. {
  9. if (u == n)
  10. {
  11. for (int i = 0; i < n; i++) puts(g[i]);
  12. puts("");
  13. return;
  14. }
  15. for (int i = 0; i < n; i++)
  16. if (!col[i] && !dg[u + i] && !udg[n - u + i])
  17. {
  18. g[u][i] = 'Q';
  19. col[i] = dg[u + i] = udg[n - u + i] = true;
  20. dfs(u + 1);
  21. col[i] = dg[u + i] = udg[n - u + i] = false;
  22. g[u][i] = '.';
  23. }
  24. }
  25. int main()
  26. {
  27. cin >> n;
  28. for (int i = 0; i < n; i++)
  29. for (int j = 0; j < n; j++)
  30. g[i][j] = '.';
  31. dfs(0);
  32. return 0;
  33. }
题解二

使用更加原始的搜索方式,搜索效率会低一些。时间复杂度为O(2^{n^2})

代码二
  1. #include <iostream>
  2. using namespace std;
  3. const int N = 20;
  4. int n;
  5. char g[N][N];
  6. bool row[N], col[N], dg[N], udg[N];
  7. void dfs(int x, int y, int s)
  8. {
  9. if (y == n) y = 0, x++;
  10. if (x == n)
  11. {
  12. if (s == n)
  13. {
  14. for (int i = 0; i < n; i++) puts(g[i]);
  15. puts("");
  16. }
  17. return;
  18. }
  19. // 不放皇后
  20. dfs(x, y + 1, s);
  21. // 放皇后
  22. if (!row[x] && !col[y] && !dg[x + y] && !udg[x - y + n])
  23. {
  24. g[x][y] = 'Q';
  25. row[x] = col[y] = dg[x + y] = udg[x - y + n] = true;
  26. dfs(x, y + 1, s + 1);
  27. row[x] = col[y] = dg[x + y] = udg[x - y + n] = false;
  28. g[x][y] = '.';
  29. }
  30. }
  31. int main()
  32. {
  33. cin >> n;
  34. for (int i = 0; i < n; i++)
  35. for (int j = 0; j < n; j++)
  36. g[i][j] = '.';
  37. dfs(0, 0, 0);
  38. return 0;
  39. }

二、BFS算法(宽度优先搜索)

例题展示

题目链接

走迷宫问题

活动 - AcWing系统讲解常用算法与数据结构,给出相应代码模板,并会布置、讲解相应的基础算法题目。icon-default.png?t=N7T8https://www.acwing.com/problem/content/description/846/

代码
  1. #include <cstring>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef pair<int, int> PII;
  6. const int N = 110;
  7. int n, m;
  8. int g[N][N];
  9. int d[N][N];
  10. PII q[N * N];
  11. int bfs()
  12. {
  13. int hh = 0, tt = 0;
  14. q[0] = {0, 0};
  15. memset(d, -1, sizeof d);
  16. d[0][0] = 0;
  17. int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
  18. while (hh <= tt)
  19. {
  20. auto t = q[hh++];
  21. for (int i = 0; i < 4; i++)
  22. {
  23. int x = t.first + dx[i], y = t.second + dy[i];
  24. if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
  25. {
  26. d[x][y] = d[t.first][t.second] + 1;
  27. q[++tt] = {x, y};
  28. }
  29. }
  30. }
  31. return d[n - 1][m - 1];
  32. }
  33. int main()
  34. {
  35. cin >> n >> m;
  36. for (int i = 0; i < n; i++)
  37. for (int j = 0; j < m; j++)
  38. cin >> g[i][j];
  39. cout << bfs() << endl;
  40. return 0;
  41. }

参考资料

  1. AcWing算法基础课
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/737273
推荐阅读
相关标签
  

闽ICP备14008679号