[문제링크 - 프로그래머스 - 네트워크] https://school.programmers.co.kr/learn/courses/30/lessons/43162
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
boolean[] visit = new boolean[n];
for(int i=0; i<computers.length; i++){
if(!visit[i]){ //방문한 적이 있는지 체크
answer++;
dfs(i, visit, computers);
}
}
return answer;
}
public void dfs(int node, boolean[] visit, int[][] computers){
visit[node] = true;
for(int i=0; i<computers.length; i++){
if(!visit[i] && computers[node][i] == 1){ //방문한 적이 없으면서 연결되어있는지 체크
dfs(i, visit, computers);
}
}
}
}
answer++;
// 네트워크 하나 추가dfs(0, visit, computers);
visit[0] = true;
// 방문한 컴퓨터에 대해 표시if(!visit[1] && computers[0][1] == 1)
// [1, 1, 0] 이므로 true