[Programmers] 네트워크(Lv.3)(C++)

Alice·2023년 8월 24일
0
post-thumbnail

풀이 소요시간 : 15분

기본적인 탐색 문제다. 연결된 노드 덩어리의 수를 묻는 문제였고, 특별한 접근법은 없었다.


전체 코드

#include <string>
#include <vector>

using namespace std;

int Ans = 0;
vector<int> Vector[201];
int Visit[201];

void Dfs(int x) {

    for(int i = 0; i < Vector[x].size(); i++)
    {
        int next = Vector[x][i];
        if(Visit[next] == 1) continue;
        Visit[next] = 1;
        Dfs(next);
    }
    
}


int solution(int n, vector<vector<int>> computers) {

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(computers[i][j] == 1) //본인 포함 연결
            {
                Vector[i].push_back(j);
            }
        }
    }
    
    for(int i = 0; i < n; i++)
    {
        if(Visit[i] == 0)
        {
            Visit[i] = 1;
            Dfs(i);
            Ans++;
        }
    }
    
    return Ans;
}
profile
SSAFY 11th

0개의 댓글