-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBFS.cpp
44 lines (38 loc) · 876 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
#include <bits/stdc++.h>
using namespace std;
void bfs(int g[][7], int start, int n)
{
int i = start;
cout << i << " ";
int visited[7] = {0};
visited[i] = 1;
queue<int> q;
q.push(i);
while (!q.empty())
{
i = q.front();
q.pop();
for (int j = 1; j < n; j++)
{
if (g[i][j] == 1 && visited[j] == 0)
{
cout << j << " ";
visited[j] = 1;
q.push(j);
}
}
};
}
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}};
bfs(g, 5, 7);
cout << "\n";
return 0;
}