문제출저 :
https://programmers.co.kr/learn/courses/30/lessons/43162
코딩테스트 연습 - 네트워크
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있
programmers.co.kr
소스코드 :
import java.util.*;
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
for(int i = 0; i < n; i++)
{
computers[i][i] = 0;
}
int count = 0;
Queue<Integer> que = new LinkedList<>();
int[] number = new int[n];
for(int i = 0; i < n; i++)
{
if (number[i] != 0) continue;
count++;
number[i] = count;
que.add(i);
while(!que.isEmpty())
{
int one = que.poll();
for(int j = 0; j < n; j++)
{
if (computers[one][j] == 1 && number[j] == 0)
{
number[j] = count;
que.add(j);
}
}
}
}
// System.out.println(Arrays.toString(number));
answer = count;
return answer;
}
}