-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path1_bfs.cpp
45 lines (39 loc) · 868 Bytes
/
1_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
#include<bits/stdc++.h>
using namespace std;
void bfs(int g[][7],int i,int n)
{
queue<int>q;
int arr[7]={0};
// start ka index 1
arr[i]=1;
cout<<i<<" ";
q.push(i);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v=1;v<n;v++)
{
if(g[u][v]==1 && arr[v]==0)
{
cout<<v<<" ";
arr[v]=1;
q.push(v);
}
}
}
}
int main()
{
int g[7][7]={{0,0,0,0,0,0,0},
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}
};
// 1 aapna start wala h aur size 7 hai iska
bfs(g,4,7);
return 0;
}