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;
    }
}

+ Recent posts