#include<iostream> #include <vector> #include <queue> #include <list> #include<algorithm> using namespace std; vector<vector<int>> adjancent; vector<bool> discovred; void CreateGraph() { adjancent = vector<vector<int>>(5); discovred = vector<bool>(5, false); adjancent[0] = { 1,2,4 }; adjancent[1] = { 0,2 }; adjancent[2] = { 0,1,3,4 }; adjancent[3] = { 2,4 }; adjancent[4] = { 0,2,3 }; } void BFS(int here) { queue<int> q; q.push(here); discovred[here] = true; cout << " 방문 " << here << endl; while (q.empty() == false) { here = q.front(); q.pop(); for (int there : adjancent[here]) { if (discovred[there] == true) continue; cout << " 방문 " << there << endl; discovred[there] = true; q.push(there); } } } int main() { CreateGraph(); BFS(0); }