알고리즘/Programmers
자바 | 프로그래머스 | 네트워크
cha-n
2021. 5. 28. 19:03
https://programmers.co.kr/learn/courses/30/lessons/43162
코딩테스트 연습 - 네트워크
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있
programmers.co.kr
Solution
그냥 dfs돌리면 된다..
class Solution {
static boolean[] visited;
public int solution(int n, int[][] computers) {
int answer = 0;
visited = new boolean[n];
for (int i=0;i<n;i++){
if (!visited[i]) {
dfs(i, computers);
answer++;
}
}
return answer;
}
void dfs(int x, int[][] computers){
visited[x] = true;
for (int i=0;i<computers[x].length;i++){
if (!visited[i] && computers[x][i]==1){
dfs(i, computers);
}
}
}
}