<C++코드>
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
65
|
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int number=7;
vector<int> a[8];
bool c[8];
void bfs(int start)
{
queue<int> q;
c[start]=true;
q.push(start);
while(!q.empty())
{
int x=q.front();
q.pop();
cout<<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(void)
{
a[1].push_back(2);
a[2].push_back(1);
a[1].push_back(3);
a[3].push_back(1);
a[2].push_back(4);
a[4].push_back(2);
a[2].push_back(5);
a[5].push_back(2);
a[4].push_back(5);
a[5].push_back(4);
a[3].push_back(6);
a[6].push_back(3);
a[3].push_back(7);
a[7].push_back(3);
a[6].push_back(7);
a[7].push_back(6);
bfs(1);
return 0;
}
|
cs |
<결과 화면>
'Algorithm' 카테고리의 다른 글
합집합 찾기(Union-Find Algorithm) (0) | 2019.11.03 |
---|---|
깊이 우선 탐색 알고리즘(Depth First Search Algorithm) (0) | 2019.11.03 |
계수 정렬 알고리즘(Counting Sort) (0) | 2019.11.02 |
STL 활용 함수 (Sort() function) (0) | 2019.11.02 |
힙 정렬 알고리즘(Heap Sort) (0) | 2019.11.02 |