Coding Test - cpp/BFS
[프로그래머스 lv2] 네트워크 - c++
에드윈H
2021. 2. 16. 14:39
programmers.co.kr/learn/courses/30/lessons/43162
#include<iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
bool check[201];
int solution(int n, vector<vector<int>> computers) {
int answer = 0;
queue<pair<int, int>> q;
for (int index = 0; index < n; index++)
{
if(check[index + 1])
continue;
q.push({ index,0 });
check[index + 1 ] = true;
answer++;
while (!q.empty())
{
int curValueX = q.front().first;
int curValueY = q.front().second;
q.pop();
for (int i = 0; i < computers[curValueX].size(); i++)
{
if (computers[curValueX][i] == 1 && check[i + 1] == false)
{
check[i + 1] = true;
q.push({ i, 0 });
}
}
}
}
return answer;
}