문제) 프로그래머스 - DFS - 네트워크
https://programmers.co.kr/learn/courses/30/lessons/43162
코딩테스트 연습 - 네트워크
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있
programmers.co.kr
0번째부터 n번째까지 DFS로 네트워크를 탐색합니다. solution 함수에서 만약 visited하지 않았던 컴퓨터면 새로운 네트워크이므로 answer에 1을 더해줍니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dfs(int n, vector<vector<int>>& computers, int now) { | |
visited[now] = true; | |
for (int i = 0; i < n; ++i) | |
if (computers[now][i] && !visited[i] && i != now) { | |
dfs(n, computers, i); | |
} | |
} |
Full Code)
GitHub - Chocochip101/Programmers
Contribute to Chocochip101/Programmers development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 프로그래머스_Programmers' 카테고리의 다른 글
[프로그래머스] 2022 카카오 공채 - 신고 결과 받기 (파이썬) 문제 및 풀이 (0) | 2022.08.04 |
---|---|
[프로그래머스] 코딩테스트 고득점 Kit - 단어 변환 (Python) 문제 및 풀이 (0) | 2021.12.07 |
[프로그래머스] 코딩테스트 고득점 Kit - 주식 가격 (C++) 문제 및 풀이 (0) | 2021.12.04 |
[프로그래머스] 코딩테스트 고득점 Kit - 위장 (Python) 문제 및 풀이 (0) | 2021.12.02 |
[프로그래머스] 코딩테스트 고득점 Kit - 더 맵게 (C++) 문제 및 풀이 (0) | 2021.12.02 |
댓글