https://school.programmers.co.kr/learn/courses/30/lessons/43162?language=java
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
쉽군요 dfs 문제입니다.
import java.util.*;
class Solution {
public static int gn;
public static int gcomputers[][];
public static boolean gbCheck[];
public static int gcnt;
public Solution(){
}
void dfs(int k){
gbCheck[k]=true;
for(int i=0;i<gcomputers[k].length;i++){
if(gbCheck[i]==false&&gcomputers[k][i]==1){
dfs(i);
}
}
}
public int solution(int n, int[][] computers) {
int answer = 0;
// 1 1 0
// 1 1 0
// 0 0 1
gcomputers=computers;
boolean bCheck[]=new boolean[computers.length];
gbCheck=bCheck;
gn=n;
Solution s=new Solution();
for(int i=0;i<n;i++){
if(gbCheck[i]==false&&gcomputers[i][i]==1){
gcnt++;
s.dfs(i);
}
}
answer=gcnt;
return answer;
}
}
'알고리즘 공부' 카테고리의 다른 글
백준 트리 (복습) (0) | 2023.10.24 |
---|---|
프로그래머스 단어변환(복습) (0) | 2023.10.20 |
프로그래머스 타겟넘버(복습) (1) | 2023.10.19 |
프로그래머스 게임맵최단거리(복습) (1) | 2023.10.19 |
프로그래머스 다리를 지나는 트럭(복습) (1) | 2023.10.18 |