forked from uhditya/Codes-for-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.cpp
53 lines (44 loc) · 865 Bytes
/
bfs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <queue>
using namespace std;
int bfs(int arr[][10],int n,int v[]){
queue<int> q;
q.push(0);
int c = 0;
for (int i = 0; i < n; i++)
{
v[i] = 0;
}
v[0] = 1;
c++;
while(c!=n){
for (int i = q.front(); i < n; i++)
{
for (int j = 0; j < n; j++)
{
if(arr[i][j]==1 && v[j]==0 ){
q.push(j);
v[j] = 1;
c++;
}
}
cout<<q.front()+1<<" ";
q.pop();
}
}
}
int main()
{
int n;
cin>>n;
int v[10],arr[10][10];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin>>arr[i][j];
}
}
bfs(arr,n,v);
return 0;
}