반응형
https://programmers.co.kr/learn/courses/30/lessons/43162
코딩테스트 연습 - 네트워크
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있
programmers.co.kr
package com.ji.study;
public class Network {
public static void main(String[] args) {
int n = 3;
int[][] com = {{1,1,0},{1,1,1},{0,1,1}};
System.out.println(solution(n, com));
}
public static int solution(int n, int[][] computers) {
int answer = 0;
// computers.length => 3
boolean[] visited = new boolean[computers.length];
for(int i=0; i<computers.length; i++) {
if(visited[i] == false) {
answer++;
dfs(i,visited,computers);
}
}
return answer;
}
static void dfs(int node, boolean[] visited, int[][] computers) {
visited[node] = true;
for(int i=0; i< computers.length; i++) {
if(visited[i] == false && computers[node][i] == 1)
dfs(i, visited, computers);
}
}
}
728x90
'[개발관련] > 코테준비' 카테고리의 다른 글
[프로그래머스] 디스크컨트롤러 (0) | 2021.09.22 |
---|---|
[프로그래머스] 여행경로 (0) | 2021.09.22 |
[프로그래머스] 체육북 (0) | 2021.08.07 |
[백준] 최소 신장 트리 (0) | 2021.08.07 |
[백준] 소문난 칠공주 (0) | 2021.08.07 |