๐Ÿ“– [๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด] Q.2667 ๋‹จ์ง€๋ฒˆํ˜ธ ๋ถ™์ด๊ธฐ

๐Ÿ“– ๋ฌธ์ œ

https://www.acmicpc.net/problem/2667

๐Ÿ” ์ ‘๊ทผ๋ฒ•

๋‹จ์ง€๋ฒˆํ˜ธ ๋ถ™์ด๊ธฐ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋ช‡๊ฐœ์˜ ๋‹จ์ง€๊ฐ€ ์žˆ๊ณ  ํ•ด๋‹น ๋‹จ์ง€๋งˆ๋‹ค ์ด ๋ช‡๊ฐ€๊ตฌ๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

bfs ํƒ์ƒ‰์„ ์ด์šฉํ•ด์„œ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ƒ   (0,-1)
ํ•˜   (0,1)
์ขŒ   (-1,0)
์šฐ   (1,0) ์ƒ ํ•˜ ์ขŒ ์šฐ ํ•œ์นธ์”ฉ์„ ํƒ์ƒ‰ํ•˜๋ฉฐ

์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ผ๋ฉด

๋ฐฉ๋ฌธ์„ ํ•˜๋ฉฐ bfs๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ƒˆ๋กœ์šด ๋‹จ์ง€์˜ ์ฒซ ์ง‘๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•ด

ํ•ด๋‹น ๋‹จ์ง€์˜ ๋ฐฉ๋ฌธํ•œ ๊ฐ€๊ตฌ ์ˆ˜๋ฅผ houseCount ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

houseCount ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€

๋‹จ์ง€์˜ ์ˆ˜๊ฐ€ ๋˜๋ฉฐ houseCount์˜ ๋ฆฌ์ŠคํŠธ์˜

๊ฐ’๋“ค์ด ๋‹จ์ง€์˜ ๊ฐ€๊ตฌ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

houseCount ์˜ ๊ธธ์ด์™€

houseCount ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌ์„ ํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋์ž…๋‹ˆ๋””.

๐Ÿ’ป ์ฝ”๋“œ

package problem.bfs.Q2667;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;

public class Main_2667 {
    public static int n;
    public static boolean [][] visited;
    public static int [][] house;
    public static int moveX[] = {0,0,-1,1};
    public static int moveY[] = {-1,1,0,0};
    public static ArrayList<Integer> houseCount = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        input();

        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(house[i][j] == 1 && !visited[i][j]){
                    bfs(i,j);
                }
            }
        }
        Collections.sort(houseCount);
        bw.write(Integer.toString(houseCount.size())+"\n");
        for(int i=0; i<houseCount.size(); i++){
            bw.write(Integer.toString(houseCount.get(i))+"\n");
        }
        bw.flush();
        bw.close();
    }
    public static void input() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        visited = new boolean[n][n];
        house = new int[n][n];

        for(int i=0; i<n; i++){
            String temp[] = br.readLine().split("");
            for(int j=0; j<n; j++){
                house[i][j] = Integer.parseInt(temp[j]);
            }
        }
        br.close();
    }

    public static void bfs (int a, int b) {
        Queue<int[]> queue = new LinkedList();
        queue.offer(new int[]{a, b});
        visited[a][b] = true;
        int count = 1;

        while (!queue.isEmpty()){
            int coordinate[] = queue.poll();
            for(int i=0; i<4; i++){
                int x = coordinate[0] + moveX[i];
                int y = coordinate[1] + moveY[i];

                if ( x < 0 || y< 0 || x>=n || y>=n)
                    continue;
                if ( house[x][y] == 0 || visited[x][y] == true)
                    continue;

                queue.offer(new int[]{x,y});
                visited[x][y] = true;
                count++;
            }
        }
        houseCount.add(count);
    }
}
Written on June 12, 2020