DFS

DFS 全称是 Depth First Search,中文名是深度优先搜索,是一种用于遍历或搜索树或图的算法。所谓深度优先,就是说每次都尝试向更深的节点走
DFS 最显著的特征在于其 递归调用自身。同时与 BFS 类似,DFS 会对其访问过的点打上访问标记,在遍历图时跳过已打过标记的点,以确保 每个点仅访问一次。符合以上两条规则的函数,便是广义上的 DFS

全排列
  1. const int N = 110;
  2. int n,a[N];
  3. bool st[N];
  4. void dfs(int u) {
  5. if (u == n) {
  6. for(int i = 0; i < n; i ++ ) {
  7. cout << a[i] << ' ';
  8. }
  9. cout << endl;
  10. return;
  11. }
  12. for (int i = 1; i <= n; i ++ ) {
  13. if(!st[i]) {
  14. st[i] = true;
  15. a[u] = i;
  16. dfs(u + 1);
  17. st[i] = false;
  18. }
  19. }
  20. }
  21. int main()
  22. {
  23. cin >> n;
  24. dfs(0);
  25. return 0;
  26. }

八皇后
  1. const int N = 110;
  2. char a[N][N];
  3. int n;
  4. bool col[N],dg[N],udg[N];
  5. void dfs(int u) {
  6. if (u == n) {
  7. for(int i = 0; i < n; i ++ ) {
  8. puts(a[i]);
  9. }
  10. cout << endl;
  11. return;
  12. }
  13. for (int i = 0; i < n; i ++ ) {
  14. if(!col[i] and !dg[u + i] and !udg[n - u + i]) {
  15. col[i] = dg[u + i] = udg[n - u + i] = true;
  16. a[u][i] = 'Q';
  17. dfs(u + 1);
  18. a[u][i] = '.';
  19. col[i] = dg[u + i] = udg[n - u + i] = false;
  20. }
  21. }
  22. }
  23. int main()
  24. {
  25. cin >> n;
  26. for (int i = 0; i < n; i ++ ) {
  27. for (int j = 0; j < n; j ++ ) {
  28. a[i][j] = '.';
  29. }
  30. }
  31. dfs(0);
  32. return 0;
  33. }

有向图的拓扑序列
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 100010;
  4. int h[N],e[N],ne[N],d[N],idx,n,m,a,b;
  5. vector<int> ans;
  6. void add(int a,int b) {
  7. e[idx] = b;
  8. ne[idx] = h[a];
  9. h[a] = idx++;
  10. }
  11. bool topsort() {
  12. queue<int> q;
  13. for(int i = 1;i<=n;i++){
  14. if(!d[i]) q.push(i);
  15. }
  16. while(!q.empty()) {
  17. int t = q.front();
  18. q.pop();
  19. ans.push_back(t);
  20. for(int i = h[t];i!=-1;i = ne[i]){
  21. int j = e[i];
  22. if(--d[j]==0) q.push(j);
  23. }
  24. }
  25. return ans.size()==n;
  26. }
  27. int main()
  28. {
  29. cin >> n >> m;
  30. memset(h, -1, sizeof h);
  31. while(m -- ) {
  32. cin >> a >> b;
  33. add(a, b);
  34. d[b] ++;
  35. }
  36. if(topsort()) {
  37. for(int i = 0;i<n;i++) {
  38. cout << ans[i] << " ";
  39. }
  40. }else{
  41. cout << -1 << endl;
  42. }
  43. return 0;
  44. }

图中点的层次
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1000010;
  4. int h[N],e[N],ne[N],d[N],idx,n,m,a,b;
  5. void add(int a,int b) {
  6. e[idx] = b;
  7. ne[idx] = h[a];
  8. h[a] = idx++;
  9. }
  10. int bfs()
  11. {
  12. queue<int> q;
  13. q.push(1);
  14. memset(d,-1,sizeof d);
  15. d[1] = 0;
  16. while(!q.empty()) {
  17. int t = q.front();
  18. q.pop();
  19. for(int i = h[t];i!=-1;i = ne[i]) {
  20. int j = e[i];
  21. if(d[j]==-1){
  22. d[j] = d[t] + 1;
  23. q.push(j);
  24. }
  25. }
  26. }
  27. return d[n];
  28. }
  29. int main()
  30. {
  31. cin >> n >> m;
  32. memset(h, -1, sizeof h);
  33. while(m -- ) {
  34. cin >> a >> b;
  35. add(a, b);
  36. }
  37. cout << bfs() << endl;
  38. return 0;
  39. }

树的重心
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 200020;
  4. int h[N],e[N],ne[N],n,idx,a,b;
  5. bool st[N];
  6. int ans = N;
  7. void add(int a,int b) {
  8. e[idx] = b;
  9. ne[idx] = h[a];
  10. h[a] = idx++;
  11. }
  12. int dfs(int u) {
  13. st[u] = true;
  14. int sum = 1,res = 0;
  15. for(int i = h[u];i!=-1;i=ne[i]) {
  16. int j = e[i];
  17. if(!st[j]){
  18. int s = dfs(j);
  19. res = max(res,s);
  20. sum += s;
  21. }
  22. }
  23. res = max(res,n-sum);
  24. ans = min(ans,res);
  25. return sum;
  26. }
  27. int main()
  28. {
  29. cin >> n;
  30. memset(h, -1, sizeof h);
  31. for(int i = 1;i<n;i++) {
  32. cin >> a >> b;
  33. add(a,b),add(b,a);
  34. }
  35. dfs(n);
  36. cout << ans << endl;
  37. return 0;
  38. }

八数码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string s,res = "12345678x";
  4. int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
  5. int bfs() {
  6. queue<string> q;
  7. q.push(s);
  8. unordered_map<string,int> d;
  9. d[s] = 0;
  10. while(!q.empty()) {
  11. auto t = q.front();
  12. q.pop();
  13. int x = t.find('x');
  14. int distance = d[t];
  15. if(t==res) return distance;
  16. for(int i = 0;i<4;i++){
  17. int a = x/3+dx[i] , b = x%3+dy[i];
  18. if(a>=0 and a<3 and b>=0 and b<3){
  19. swap(t[x],t[3*a+b]);
  20. if(!d.count(t)){
  21. d[t] = distance + 1;
  22. q.push(t);
  23. }
  24. swap(t[x],t[3*a+b]);
  25. }
  26. }
  27. }
  28. return -1;
  29. }
  30. int main()
  31. {
  32. char ch;
  33. for(int i = 0;i<9;i++) {
  34. cin >> ch;
  35. s += ch;
  36. }
  37. cout << bfs() << endl;
  38. return 0;
  39. }