https://school.programmers.co.kr/learn/courses/30/lessons/43162
풀이
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
boolean[][] visited = new boolean[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(computers[i][j] == 1 && !visited[i][j]){
dfs(i, j, visited, computers);
answer++;
}
}
}
return answer;
}
public static void dfs(int i, int j, boolean visited[][], int[][] computers){
visited[i][j] = true;
for(int k = 0; k < computers.length; k++){
if(computers[j][k] == 1 && !visited[j][k]){
dfs(j, k, visited, computers);
}
}
}
}
dfs를 이용해 간단하게 answer++만 처리해주는 방식으로 진행하였다.
'문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 단어 변환(JAVA) (0) | 2024.05.06 |
---|---|
[프로그래머스] 카펫(JAVA) (0) | 2024.05.05 |
[프로그래머스] 게임 맵 최단거리(JAVA) (0) | 2024.05.04 |
[프로그래머스] 모의고사(JAVA) (0) | 2024.04.29 |
[프로그래머스] H-Index(JAVA) (1) | 2024.04.28 |