-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.cpp
64 lines (53 loc) · 924 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
54
55
56
57
58
59
60
61
62
63
64
/*
* @brief BFS 탐색 방법을 익히기 위한 기본 예제 코드
* @date 2021/08/17
* @author 강동훈
* @parameter nothing
* @return nothing
* @big O
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int num = 7;
bool c[7];
vector<int> a[8];
void BFS(int start) {
queue<int> q;
q.push(start);
c[start] = true;
while (!q.empty()) {
int x = q.front();
q.pop();
printf("%d ", x);
for (int i = 0; i < a[x].size(); i++) {
int y = a[x][i];
if (!c[y]) {
q.push(y);
c[y] = true;
}
}
}
}
int main() {
a[1].push_back(2);
a[1].push_back(3);
a[2].push_back(1);
a[2].push_back(4);
a[2].push_back(5);
a[3].push_back(1);
a[3].push_back(6);
a[3].push_back(7);
a[4].push_back(2);
a[4].push_back(5);
a[5].push_back(2);
a[5].push_back(4);
a[6].push_back(3);
a[6].push_back(7);
a[7].push_back(3);
a[7].push_back(6);
// start from 1(start node)
BFS(1);
return 0;
}