코딩공작소

[프로그래머스]네크워크 본문

알고리즘/그래프

[프로그래머스]네크워크

안잡아모찌 2019. 10. 26. 00:23

https://programmers.co.kr/learn/courses/30/lessons/43162

 

코딩테스트 연습 - 네트워크 | 프로그래머스

네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있을 때 컴퓨터 A와 컴퓨터 C도 간접적으로 연결되어 정보를 교환할 수 있습니다. 따라서 컴퓨터 A, B, C는 모두 같은 네트워크 상에 있다고 할 수 있습니다. 컴퓨터의 개수 n, 연결에 대한 정보가 담긴 2차원 배열 computers가 매개변수로 주어질 때, 네트워크

programmers.co.kr

1차원에서 dfs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>
#include <vector>
 
using namespace std;
 
void dfs(int start,vector<vector<int>> computers,bool *isV){
    isV[start]=true;
    for(int i=0;i<computers.size();i++){
        if(!isV[i] && computers[start][i]==1){
            dfs(i,computers,isV);
        }
    }
}
 
int solution(int n, vector<vector<int>> computers) {
    int answer = 0;
    bool *isV=new bool[computers.size()];
    for(int i=0;i<computers.size();i++) isV[i]=false;
    for(int i=0;i<computers.size();i++){
        if(!isV[i]){
            answer++;
            dfs(i,computers,isV);
        }
    }
    return answer;
}
cs

'알고리즘 > 그래프' 카테고리의 다른 글

[프로그래머스]여행경로  (0) 2019.10.27
[프로그래머스]단어변환  (0) 2019.10.26
[백준]말이 되고픈 원숭이  (0) 2019.10.09
[백준]달이차오른다,가자  (0) 2019.10.08
[백준]영역구하기  (0) 2019.10.08