BFS
장점
단점
수행 과정
구현
void BFS(int start){
// 시작점에서 갈 수 있는 모든 정점 큐에 담기
// 큐 가장 앞에 있는 얘로 BFS 다시 시작.
// BFS는 큐가 비어있을 때까지..
qu.push(start);
isVisited[start] = true;
while(!qu.empty()){
int from = qu.front();
qu.pop();
printf("%d ", from);
for(int i = 0; i<graph[from].size(); i++){
int to = graph[from][i];
if(!isVisited[to]){
qu.push(to);
isVisited[to] = true;
}
}
}
}
void main(){
int from, to, start;
scanf("%d %d %d", &N, &M, &start);
for (int i = 0; i<M; i++){
scanf("%d %d", &from, &to);
graph[from].push_back(to);
graph[to].push_back(from);
}
// fill_n(isVisited, 1001, false);
BFS(start);
}Issue
Last updated
Was this helpful?