数据结构-图的遍历——DFS深度优先搜索_在线工具

2022年5月2日09:26:59

题目链接:https://www.dotcpp.com/oj/problem1702.html?sid=7509471&lang=1#editor

模板题,dfs,dfs比bfs还简单,这个题是简单的dfs思想,本来不想拿出来了,但既然做了,那就留给以后的人来学习把。

Talk is cheap. Show me the code.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int num=100;
 4 int n;
 5 int graph[num][num];//邻接矩阵
 6 bool vis[num];//标记数组
 7 int cnt;
 8 void dfs(int s)
 9 {
10     vis展开=1;//上来标记
11     cout<<s<<" ";//打印走过的
12     for(register int i=0;i<n;i++)
13     {
14         if(!vis[i]&&graph展开[i])//满足条件
15         {
16             vis[i]=1;
17             dfs(i);//下一层
18         }
19     }
20 }
21 int main()
22 {
23     std::ios::sync_with_stdio(false);
24     cin>>n;
25     for(register int i=0;i<n;i++)
26     {
27         for(register int j=0;j<n;j++)
28         {
29             cin>>graph[i][j];
30         }
31     }
32     dfs(0);//从0开始搜
33     return 0;
34 }

 

  • 作者:江上舟摇
  • 原文链接:https://www.cnblogs.com/LQS-blog/p/16206971.html
    更新时间:2022年5月2日09:26:59 ,共 611 字。