본문 바로가기
Coding Test/Programmers

[Programmers] (깊이/너비 우선 탐색(DFS/BFS)) Lv 3. 네트워크

by song.ift 2024. 10. 23.

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

#include <string>
#include <vector>

using namespace std;

vector<bool> visited;

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

int solution(int n, vector<vector<int>> computers) {
    //24.10.23
    int answer = 0;
    visited.resize(n);

    for (int i = 0; i < n; ++i)
    {
        if (!visited[i])
        {
            dfs(i, computers);
            ++answer;
        }
    }
    return answer;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/commit/784cca56961173c48cd7ccafb6ebbd0553f2344a

 

[level 3] Title: 네트워크, Time: 0.07 ms, Memory: 4.2 MB -BaekjoonHub · developeSHG/Algorithm-Baekjoon_Programmers@784cca5

developeSHG committed Oct 23, 2024

github.com

 

댓글