λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
ProgramSolve/Baekjoon

[BOJ] 2667번 - λ‹¨μ§€λ²ˆν˜ΈλΆ™μ΄κΈ°(JAVA)

by SooooooooS 2023. 3. 23.
728x90

πŸ› οΈ 문제 πŸ› οΈ 

문제
μž…μΆœλ ₯


πŸ—’οΈ μ„€λͺ… πŸ—’οΈ 

  1. μ‹œμž‘ 정점 μ°ΎκΈ°
    1. 이미지 μƒμœΌλ‘œ ν™•μΈν–ˆμ„ λ•Œ μ™Όμͺ½ μœ„ > 였λ₯Έμͺ½ μœ„ > μ™Όμͺ½ μ•„λž˜ > 였λ₯Έμͺ½ μ•„λž˜ 와 같이 Z λͺ¨μ–‘μœΌλ‘œ μ‹œμž‘ 정점을 μ°ΎλŠ”λ‹€.
    2. ν˜„μž¬ μœ„μΉ˜μ˜ 값이 1μ΄λ©΄μ„œ λ°©λ¬Έν•˜μ§€ μ•ŠμœΌλ©΄ μ‹œμž‘ 정점이닀.
  2. μ‹œμž‘ 정점을 κΈ°μ€€μœΌλ‘œ κ·Έλž˜ν”„ 탐색
    1. BFS κ·Έλž˜ν”„ 탐색 μ‚¬μš©
    2. λŒ€κ°μ„ μ„ μ œμ™Έν•œ μƒν•˜μ’Œμš° μœ„μΉ˜μ— μ•„νŒŒνŠΈκ°€ 있으면 ν˜„μž¬ 단지에 μžˆλŠ” μ•„νŒŒνŠΈ 수 증가
    3. BFS 탐색이 λλ‚˜λ©΄ νƒμƒ‰ν•œ μ•„νŒŒνŠΈ 수 λ°˜ν™˜
  3. 탐색이 λλ‚˜κ³  아직 남은 정점이 있으면 β‘ ,β‘‘λ²ˆ κ³Όμ • 반볡
  4. λͺ¨λ“  μ •μ μ˜ 탐색이 λλ‚˜λ©΄ μ˜¬λ°”λ₯Έ ν˜•μ‹μœΌλ‘œ 좜λ ₯ ν›„ ν”„λ‘œκ·Έλž¨ μ’…λ£Œ

β˜… μ•„νŒŒνŠΈ 단지내에 μžˆλŠ” μ•„νŒŒνŠΈ 수λ₯Ό μ €μž₯ν•˜λŠ” λ°°μ—΄(aparts[]) μ΄ˆκΈ°ν™” β˜…

N이 ν™€μˆ˜μΌ 경우 κ°€μ§ˆ 수 μžˆλŠ” μ•„νŒŒνŠΈ 단지 μ΅œλŒ€ 수
N이 짝수일 경우 κ°€μ§ˆ 수 μžˆλŠ” μ•„νŒŒνŠΈ 단지 μ΅œλŒ€ 수


πŸ€ 풀이 μ½”λ“œ πŸ€

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class BOJ2667 {
    public static int[][] complex; //맡
    public static boolean[][] visited; //λ°©λ¬Έμ—¬λΆ€ 확인 λ°°μ—΄
    public static int[] aparts; //μ•„νŒŒνŠΈ 단지에 μžˆλŠ” μ•„νŒŒνŠΈ 수 μ €μž₯
    public static int aprtnum; //μ•„νŒŒνŠΈ 단지 번호

    static int BFS(int vx, int vy){
        Queue<int[]> queue = new LinkedList<>();

        //μƒν•˜μ’Œμš°λ₯Ό ν™•μΈν•˜κΈ° μœ„ν•œ λ°°μ—΄
        int[] X = {-1, 1, 0, 0};
        int[] Y = {0, 0, -1, 1};

        queue.offer(new int[] {vx, vy});
        visited[vx][vy] = true;
        int count = 1;
        while(!queue.isEmpty()) {
            int[] poll = queue.poll();
            for (int t = 0; t < 4; t++) {
                int x = poll[0] + X[t];
                int y = poll[1] + Y[t];
                if (x >= 0 && x < complex.length && y >= 0 && y < complex.length) {
                    if (complex[x][y] == 1 && !visited[x][y]) {
                        complex[x][y] = aprtnum;
                        queue.offer(new int[]{x, y});
                        visited[x][y] = true;
                        count++;
                    }
                }
            }
        }
        return count;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(bf.readLine()); //μ§€λ„μ˜ 크기

        complex = new int[N][N];
        visited = new boolean[N][N];
        /** μ§€λ„μ˜ ν¬κΈ°μ—μ„œ λ§Œλ“€μ–΄μ§ˆ 수 μžˆλŠ” μ΅œλŒ€μ˜ λ‹¨μ§€μˆ˜
         *  N이 짝수일 경우 : N*N/2 + 1
         *  N이 ν™€μˆ˜μΌ 경우 : (N*N+1)/2 + 1
         *  λ‘˜ 쀑 ν™€μˆ˜μΌ 경우의 식이 더 μ—¬μœ λ‘œμš°λ―€λ‘œ 선택
         */
        int size = ((N*N)+1)/2 + 1;
        aparts = new int[size];
        aprtnum = 0;
        for(int i = 0; i < N; i++) {
            String[] s = bf.readLine().split("");
            for(int j = 0; j < N; j++) {
                complex[i][j] = Integer.parseInt(s[j]);
            }
        }
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                if(complex[i][j] == 1 && !visited[i][j]){
                    aprtnum++;
                    aparts[aprtnum] = BFS(i, j);
                }
            }
        }
        Arrays.sort(aparts);
        System.out.println(aprtnum);
        for(int i = 0; i < aparts.length; i++){
            if(aparts[i] != 0)
                System.out.println(aparts[i]);
        }
    }
}
728x90