圖的廣度優先搜索

pay attention to the cout

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 1010;
int nV,nE;
int vis[maxn];
int map[maxn][maxn];


int main()  {
    int st;
    cin>>nV>>st;
    for (int i=1;i<=nV;i++)
     for (int j=1;j<=nV;j++) {
        cin>>map[i][j];
     }
     queue<int >Q;
     Q.push(st);
     vis[st] = 1;
     while(!Q.empty()) {
        int tmp = Q.front();Q.pop();
        for (int i=1;i<=nV;i++) {
            if(map[tmp][i]&&!vis[i]) {
                vis[i] = 1;
                Q.push(i);
            }
        }
        cout<<tmp;
        if(!Q.empty()) cout<<" ";
     }
}

“`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章