-
69. 이진트리 너비 우선 탐색( 큐 자료구조 직접구현 : BFS)알고리즘 문제풀기/인프런 강의 정답 2022. 5. 17. 12:46
나의정답.
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <iostream> #include <string> #include <algorithm> #include <vector> #include <stack> using namespace std; int Queue[100], front = -1, back = -1, ch[10]; vector<int> map[10]; int main(int argc, char** argv) { //freopen("input.txt", "rt", stdin); int a, b, x; for (int i = 1; i <= 6; i++) { scanf("%d %d", &a, &b); map[a].push_back(b); map[b].push_back(a); } Queue[++back] = 1; ch[1] = 1; while (front < back) { x = Queue[++front]; printf("%d ", x); for (int i = 0; i < map[x].size(); i++) { if (ch[map[x][i]] == 0) { ch[map[x][i]] = 1; Queue[++back] = map[x][i]; } } } return 0; }
'알고리즘 문제풀기 > 인프런 강의 정답' 카테고리의 다른 글
71. 송아지 찾기 (BFS : 상태트리탐색) (0) 2022.05.19 70. 그래프 최단거리 (BFS: Breadth First Search) (0) 2022.05.19 68. 최소비용(인접리스트) (0) 2022.05.16 67. 최소 비용 (그래프 DFS) (0) 2022.05.15 65. 미로탐색 (2차원 배열 DFS) (0) 2022.05.13