在c ++中使用队列的BFS代码无法工作......它执行不需要的结果

时间:2017-06-06 08:46:07

标签: c++

#include<iostream>
#include<queue>
using namespace std;
int adj_mat[1000][1000];
bool visited[1000];
int main()
{
    int v,e,x,y;
    queue<int>q;
    cout<<"enter number of vertices and edges:\n";
    cin>>v>>e;
    while(e--)
    {
        cin>>x>>y;
        adj_mat[x][y]=adj_mat[y][x]=1;
    }
    cout<<"enter the start vertex:\n";
    cin>>x;
    visited[x]=true;
    q.push(x);
    while(!q.empty()){
        x=q.front();
        q.pop();
        for( int i=1;i<=v;i++){
            if(adj_mat[x][i]!=0 && !visited[i]){
                cout<<i<<",";
                q.push(i);
                visited[i]=true;
                }
        }
    }
    return 0;
}

这是在c ++中使用队列的图表的BFS代码...但是这个代码中存在一些错误,它会产生不需要的结果。 如果我把输入作为: 顶点:4 边:4 边: 1 2 3 4 2 4 3 1 初始顶点2 它显示输出1,4,3 但是......不正确......帮助我...

0 个答案:

没有答案