I have written code for BFS in c++ but it is not working correctly. I cross-checked it but I can't find my mistake. Test case input is 5 8 0 1 0 4 1 2 2 0 2 4 3 0 3 2 4 3
My Output 0 1 4 2 3
Correct Output 0 1 2 3 4
void bfs(int V, vector<int>arr[]){
bool vis[V];
for(int i=0;i<V;i++) vis[i]=false;
queue<int>q;
q.push(0);
vis[0]=true;
while(!q.empty()){
int t = q.front();
q.pop();
cout<<t<<" ";
for(int x : arr[t]){
if(vis[x]==true) continue;
q.push(x);
vis[x]=true;
}
}
}
Aucun commentaire:
Enregistrer un commentaire