只有一行,包含n个整数,表示按照题目描述中的广度优先遍历算法遍历整个图的访问顶点顺序。每个整数后输出一个空格,并请注意行尾输出换行。
4 0 0 0 1 0 0 1 1 0 1 0 1 1 1 1 0
0 3 1 2
#include <bits/stdc++.h> using namespace std; int graph[55][55]; int vis[55]; int ans[55]; int main() { int n; while(cin>>n) { memset(graph,0,sizeof(graph)); memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) cin>>graph[i][j]; } int h=0,t=0; vis[t]=1; ans[t]=0; t++; while(h<t&&t<=n) { int c; c=ans[h]; for(int i=0;i<n;i++) { if(graph[c][i]==1&&vis[i]==0) { ans[t]=i; vis[i]=1; t++; } if(t>n) break; } h++; } for(int i=0;i<n;i++) { cout<<ans[i]<<‘ ‘; } cout<<endl; } return 0; }
原文:https://www.cnblogs.com/cuiiiii/p/13215864.html