알고리즘/프로그래머스

프로그래머스 - 네트워크

시나모온 2020. 8. 31. 05:17

문제 링크입니다 : https://programmers.co.kr/learn/courses/30/lessons/43162

 

코딩테스트 연습 - 네트워크

네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있��

programmers.co.kr

 

 

dfs or bfs

 

 

 

 

 

#include <string>
#include <vector>
#include <iostream>

using namespace std;

vector<bool> visited;
int N;

void dfs(const vector<vector<int>>& computers, int here) {
    if(visited[here]) return;
    visited[here] = true;
    
    for(int i = 0; i < N; i++) {
        if(!computers[here][i]) continue;
        if(here == i || visited[i]) continue;
        dfs(computers, i);
    }
    
}

int solution(int n, vector<vector<int>> computers) {
    int answer = 0;
    
    N = n;
    visited.assign(n, false);
    
    for(int i = 0; i < n; i++) {
        if(visited[i]) continue;
        dfs(computers, i);
        answer++;
    }
    
    
    return answer;
}

 

 

 

개발 환경 : vscode

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~