赞
踩
- DFS、BFS都可以对整个问题空间进行搜索,搜索的结构都是像一棵树。
- DFS会尽可能往深搜,当搜索到叶节点时就会回溯。而BFS每一次只会扩展一层。
DFS与BFS的区别:
搜索方式 | 数据结构 | 空间复杂度 | 性质 |
DFS | 栈 | O(h),其中h为搜索空间树的高度 | 不具有最短性 |
BFS | 队列 | 有“最短路”概念 |
活动 - AcWing系统讲解常用算法与数据结构,给出相应代码模板,并会布置、讲解相应的基础算法题目。https://www.acwing.com/problem/content/844/
搜索顺序可以看每个位置填哪些数,属于全排列问题。
- #include <iostream>
-
- using namespace std;
-
- const int N = 10;
-
- int n;
- int path[N];
- bool st[N];
-
- void dfs(int u)
- {
- if (u == n)
- {
- for (int i = 0; i < n; i++) printf("%d ", path[i]);
- puts("");
- return;
- }
-
- for (int i = 1; i <= n; i++)
- if (!st[i])
- {
- path[u] = i;
- st[i] = true;
- dfs(u + 1);
- st[i] = false;
- }
- }
-
- int main()
- {
- cin >> n;
-
- dfs(0);
-
- return 0;
- }

活动 - AcWing系统讲解常用算法与数据结构,给出相应代码模板,并会布置、讲解相应的基础算法题目。https://www.acwing.com/problem/content/845/
每一行只能有一个皇后,类似于全排列问题。时间复杂度为
。
- #include <iostream>
-
- using namespace std;
-
- const int N = 20;
-
- int n;
- char g[N][N];
- bool col[N], dg[N], udg[N];
-
- void dfs(int u)
- {
- if (u == n)
- {
- for (int i = 0; i < n; i++) puts(g[i]);
- puts("");
- return;
- }
-
- for (int i = 0; i < n; i++)
- if (!col[i] && !dg[u + i] && !udg[n - u + i])
- {
- g[u][i] = 'Q';
- col[i] = dg[u + i] = udg[n - u + i] = true;
- dfs(u + 1);
- col[i] = dg[u + i] = udg[n - u + i] = false;
- g[u][i] = '.';
- }
- }
-
- int main()
- {
- cin >> n;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- g[i][j] = '.';
-
- dfs(0);
-
- return 0;
- }

使用更加原始的搜索方式,搜索效率会低一些。时间复杂度为
。
- #include <iostream>
-
- using namespace std;
-
- const int N = 20;
-
- int n;
- char g[N][N];
- bool row[N], col[N], dg[N], udg[N];
-
- void dfs(int x, int y, int s)
- {
- if (y == n) y = 0, x++;
-
- if (x == n)
- {
- if (s == n)
- {
- for (int i = 0; i < n; i++) puts(g[i]);
- puts("");
- }
- return;
- }
-
- // 不放皇后
- dfs(x, y + 1, s);
-
- // 放皇后
- if (!row[x] && !col[y] && !dg[x + y] && !udg[x - y + n])
- {
- g[x][y] = 'Q';
- row[x] = col[y] = dg[x + y] = udg[x - y + n] = true;
- dfs(x, y + 1, s + 1);
- row[x] = col[y] = dg[x + y] = udg[x - y + n] = false;
- g[x][y] = '.';
- }
- }
-
- int main()
- {
- cin >> n;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- g[i][j] = '.';
-
- dfs(0, 0, 0);
-
- return 0;
- }

走迷宫问题
- #include <cstring>
- #include <iostream>
- #include <algorithm>
-
- using namespace std;
-
- typedef pair<int, int> PII;
-
- const int N = 110;
-
- int n, m;
- int g[N][N];
- int d[N][N];
- PII q[N * N];
-
- int bfs()
- {
- int hh = 0, tt = 0;
- q[0] = {0, 0};
-
- memset(d, -1, sizeof d);
- d[0][0] = 0;
-
- int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
-
- while (hh <= tt)
- {
- auto t = q[hh++];
-
- for (int i = 0; i < 4; i++)
- {
- int x = t.first + dx[i], y = t.second + dy[i];
- if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
- {
- d[x][y] = d[t.first][t.second] + 1;
- q[++tt] = {x, y};
- }
- }
- }
-
- return d[n - 1][m - 1];
- }
-
- int main()
- {
- cin >> n >> m;
-
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- cin >> g[i][j];
-
- cout << bfs() << endl;
-
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。