알고리즘 문제풀기/인프런 강의 정답

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;
}