문제 : https://programmers.co.kr/learn/courses/30/lessons/43162
코딩테스트 연습 - 네트워크 | 프로그래머스
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있을 때 컴퓨터 A와 컴퓨터 C도 간접적으로 연결되어 정보를 교환할 수 있습니다. 따라서 컴퓨터 A, B, C는 모두 같은 네트워크 상에 있다고 할 수 있습니다. 컴퓨터의 개수 n, 연결에 대한 정보가 담긴 2차원 배열 computers가 매개변수로 주어질 때, 네트워크
programmers.co.kr
단순히 DFS를 통해서 몇번의 DFS를 하는지를 측정하면 된다.
그 탐색 횟수가 결과적으로 연결된 네트워크의 수가 되는 것이다.
#include <string>
#include <vector>
using namespace std;
bool check[200]={false,};
void dfs(int idx, vector<vector<int>> computers){
check[idx]=true;
for(int i=0;i<=computers[idx].size();i++){
if(check[i]==false && computers[idx][i]==1)
dfs(i, computers);
}
}
int all_check(int n){
for(int i=0;i<n;i++)
if(!check[i])
return i;
return -1;
}
int solution(int n, vector<vector<int>> computers) {
int answer = 0;
while(true){
int now = all_check(n);
if(now==-1)
break;
else
dfs(now, computers);
answer++;
}
return answer;
}
'Archived(CSE Programming) > 알고리즘(C++)' 카테고리의 다른 글
프로그래머스_NQueen (0) | 2019.10.09 |
---|---|
프로그래머스_예산 (0) | 2019.10.08 |
백준 9019_DSLR (0) | 2019.10.02 |
백준 13913_숨바꼭질 4 (0) | 2019.10.02 |
브루트포스_연습2 (0) | 2019.09.26 |