Java/프로그래머스
[Java] 프로그래머스 [Level-3] 네트워크
SeungyubLee
2023. 1. 1. 12:20
public class Solution {
// 네트워크
static boolean[] visited = {};
public static void dfs(int i, int[][] computers) {
visited[i] = true;
for(int j = 0; j < computers[i].length; j++) {
if(!visited[j] && computers[i][j] == 1) {
dfs(j, computers);
}
}
}
public static int solution(int n, int[][] computers) {
int answer = 0;
visited = new boolean[n];
for(int i = 0; i < n; i++) {
if(!visited[i]) {
answer++;
dfs(i, computers);
}
}
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 3; // 3대의 컴퓨터
int[][] computers = {{1, 1, 0}, {1, 1, 0}, {0, 0, 1}}; // 연결된 네트워크
System.out.println(solution(n, computers)); // 2
}
}
프로그래머스 네트워크 문제 풀이 Java