题目链接: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[s]=1;//上来标记
11 cout<<s<<" ";//打印走过的
12 for(register int i=0;i<n;i++)
13 {
14 if(!vis[i]&&graph[s][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 }
正文完