#include<bits/stdc++.h> using namespace std; int a[11][11]; bool visited[11]; void store_graph(){//邻接矩阵存储图 int i,j; for(i = 1; i <= 10; i++) for(j = 1; j <= 10; j++) cin>>a[i][j]; } void dfs_graph(){ void dfs(int v); memset(visited,false,sizeof(visited)); for(int i = 1; i <= 10; i++) if(visited[i] == false) dfs(i); } int adj(int x){ for(int i =1; i <=10; i++){ if(a[x][i] == 1&& visited[i] == false) return i; return 0; } } void dfs(int v){ int adj(int x); cout<<v<<' '; visited[v]=true; int adj = adj(v); while(adj != 0){ if(visited[adj] == false) dfs(adj); adj = adj(v); } } int main(){ cout<<"初始化图:"<<endl; store_graph(); cout<<"dfs遍历结果:"<<endl; dfs_graph(); return 0; }
dfs
0 条评论