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
'Java > 프로그래머스' 카테고리의 다른 글
| [Java] 프로그래머스 [Level-2] 요격 시스템 (0) | 2023.09.06 | 
|---|---|
| [Java] 프로그래머스 [Level-3] 단어 변환 (0) | 2022.12.31 | 
| [Java] 프로그래머스 [Level-2] 디펜스 게임 (0) | 2022.12.27 | 
| [Java] 프로그래머스 [Level-3] 숫자 타자 대회 (0) | 2022.12.13 | 
| [Java] 프로그래머스 [Level-4] 쌍둥이 빌딩 숲 (0) | 2022.12.06 | 
 
                    
                   
                    
                   
                    
                  