Information Security
[프로그래머스] 네트워크 본문
[프로그래머스] 네트워크 문제풀이 (Java)
[프로그래머스] 네트워크 문제풀이 (Java)
velog.io
재귀함수를 통한 DFS
참고한 풀이
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
boolean[] visited = new boolean[n];
for(int i=0; i<n; i++){
if(!visited[i]){
dfs(computers, visited, i);
answer++;
}
}
return answer;
}
public void dfs(int[][]computers, boolean[]visited, int v){
visited[v] = true;
for(int j=0; j<computers.length; j++){
if(computers[v][j]==1 && !visited[j]){
dfs(computers, visited, j);
}
}
}
}
'STUDY > Coding' 카테고리의 다른 글
[백준] 미로 탐색 (0) | 2024.03.22 |
---|---|
[프로그래머스] 게임 맵 최단거리 (0) | 2024.03.22 |
[프로그래머스] 타켓 넘버 (0) | 2024.03.22 |
동적(다이나믹) 프로그래밍 (0) | 2021.11.29 |
이진탐색 (0) | 2021.11.17 |