DFS
DFS 全称是 Depth First Search,中文名是深度优先搜索,是一种用于遍历或搜索树或图的算法。所谓深度优先,就是说每次都尝试向更深的节点走
DFS 最显著的特征在于其 递归调用自身。同时与 BFS 类似,DFS 会对其访问过的点打上访问标记,在遍历图时跳过已打过标记的点,以确保 每个点仅访问一次。符合以上两条规则的函数,便是广义上的 DFS
全排列
const int N = 110;int n,a[N];bool st[N];void dfs(int u) {if (u == n) {for(int i = 0; i < n; i ++ ) {cout << a[i] << ' ';}cout << endl;return;}for (int i = 1; i <= n; i ++ ) {if(!st[i]) {st[i] = true;a[u] = i;dfs(u + 1);st[i] = false;}}}int main(){cin >> n;dfs(0);return 0;}
八皇后
const int N = 110;char a[N][N];int n;bool col[N],dg[N],udg[N];void dfs(int u) {if (u == n) {for(int i = 0; i < n; i ++ ) {puts(a[i]);}cout << endl;return;}for (int i = 0; i < n; i ++ ) {if(!col[i] and !dg[u + i] and !udg[n - u + i]) {col[i] = dg[u + i] = udg[n - u + i] = true;a[u][i] = 'Q';dfs(u + 1);a[u][i] = '.';col[i] = dg[u + i] = udg[n - u + i] = false;}}}int main(){cin >> n;for (int i = 0; i < n; i ++ ) {for (int j = 0; j < n; j ++ ) {a[i][j] = '.';}}dfs(0);return 0;}
有向图的拓扑序列
#include <bits/stdc++.h>using namespace std;const int N = 100010;int h[N],e[N],ne[N],d[N],idx,n,m,a,b;vector<int> ans;void add(int a,int b) {e[idx] = b;ne[idx] = h[a];h[a] = idx++;}bool topsort() {queue<int> q;for(int i = 1;i<=n;i++){if(!d[i]) q.push(i);}while(!q.empty()) {int t = q.front();q.pop();ans.push_back(t);for(int i = h[t];i!=-1;i = ne[i]){int j = e[i];if(--d[j]==0) q.push(j);}}return ans.size()==n;}int main(){cin >> n >> m;memset(h, -1, sizeof h);while(m -- ) {cin >> a >> b;add(a, b);d[b] ++;}if(topsort()) {for(int i = 0;i<n;i++) {cout << ans[i] << " ";}}else{cout << -1 << endl;}return 0;}
图中点的层次
#include <bits/stdc++.h>using namespace std;const int N = 1000010;int h[N],e[N],ne[N],d[N],idx,n,m,a,b;void add(int a,int b) {e[idx] = b;ne[idx] = h[a];h[a] = idx++;}int bfs(){queue<int> q;q.push(1);memset(d,-1,sizeof d);d[1] = 0;while(!q.empty()) {int t = q.front();q.pop();for(int i = h[t];i!=-1;i = ne[i]) {int j = e[i];if(d[j]==-1){d[j] = d[t] + 1;q.push(j);}}}return d[n];}int main(){cin >> n >> m;memset(h, -1, sizeof h);while(m -- ) {cin >> a >> b;add(a, b);}cout << bfs() << endl;return 0;}
树的重心
#include <bits/stdc++.h>using namespace std;const int N = 200020;int h[N],e[N],ne[N],n,idx,a,b;bool st[N];int ans = N;void add(int a,int b) {e[idx] = b;ne[idx] = h[a];h[a] = idx++;}int dfs(int u) {st[u] = true;int sum = 1,res = 0;for(int i = h[u];i!=-1;i=ne[i]) {int j = e[i];if(!st[j]){int s = dfs(j);res = max(res,s);sum += s;}}res = max(res,n-sum);ans = min(ans,res);return sum;}int main(){cin >> n;memset(h, -1, sizeof h);for(int i = 1;i<n;i++) {cin >> a >> b;add(a,b),add(b,a);}dfs(n);cout << ans << endl;return 0;}
八数码
#include <bits/stdc++.h>using namespace std;string s,res = "12345678x";int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};int bfs() {queue<string> q;q.push(s);unordered_map<string,int> d;d[s] = 0;while(!q.empty()) {auto t = q.front();q.pop();int x = t.find('x');int distance = d[t];if(t==res) return distance;for(int i = 0;i<4;i++){int a = x/3+dx[i] , b = x%3+dy[i];if(a>=0 and a<3 and b>=0 and b<3){swap(t[x],t[3*a+b]);if(!d.count(t)){d[t] = distance + 1;q.push(t);}swap(t[x],t[3*a+b]);}}}return -1;}int main(){char ch;for(int i = 0;i<9;i++) {cin >> ch;s += ch;}cout << bfs() << endl;return 0;}
