-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS_기초.cpp
54 lines (47 loc) · 820 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
/*
너비우선 탐색 : 최단경로 구할때 많이 쓰임, 미로찾기
*/
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int visit[8];
vector<int> a[8];
void BFS(int start)
{
queue<int> q;
q.push(start);
visit[start]=true;
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(!visit[y]){
q.push(y);
visit[y]=true;
}
}
}
}
int main()
{
/*
a[1] = 2 3
a[2] = 1 3 4 5
a[3] = 1 2 6 7
a[4] = 2 5
a[5] = 2 4
a[6] = 3 7
a[7] = 3 6
*/
int n;
cin >> n;
for(int i=0;i<n;i++){
int num,node;
cin >> node >> num;
a[node].push_back(num);
}
BFS(1);
}