๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 2583๋ฒˆ ์˜์—ญ ๊ตฌํ•˜๊ธฐ ์ž๋ฐ” ๋ฌธ์ œํ’€์ด

by Wonit 2021. 8. 2.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 2583๋ฒˆ ์˜์—ญ ๊ตฌํ•˜๊ธฐ ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

2583๋ฒˆ: ์˜์—ญ ๊ตฌํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— M๊ณผ N, ๊ทธ๋ฆฌ๊ณ  K๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. M, N, K๋Š” ๋ชจ๋‘ 100 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ K๊ฐœ์˜ ์ค„์—๋Š” ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ง์‚ฌ๊ฐํ˜•์˜ ์™ผ์ชฝ ์•„๋ž˜ ๊ผญ์ง“์ ์˜ x, y์ขŒํ‘œ๊ฐ’๊ณผ ์˜ค

www.acmicpc.net

 

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด์ž.

 

๋ฌธ์ œ ์ ‘๊ทผ

 

์ด๋ฒˆ ๋ฌธ์ œ๋Š” BFS ๋‚˜ DFS ๋ชจ๋‘ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ค‘์š”ํ•œ ํŒ์€ ๋ชจ๋ˆˆ ์ข…์ด๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐ”๋ผ๋ณผ ๊ฒƒ์ด๋ƒ? ์˜€๋˜๊ฒƒ ๊ฐ™๋‹ค.

 

๋งŒ์•ฝ ๋ฐฐ์—ด๋กœ ์ƒ๊ฐ์„ ํ•œ๋‹ค๋ฉด ๋ชจ๋ˆˆ ์ข…์ด๋ฅผ 2๊ฐ€์ง€ ๊ด€์ ? ์œผ๋กœ ๋ฐ”๋ผ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

  1. ๊ฐ๊ฐ ๊ผญ์ง€์ ์„ ์ขŒํ‘œ๋กœ ๋ณผ ๊ฒƒ์ธ๊ฐ€?
  2. ํ•˜๋‚˜์˜ ์‚ฌ๊ฐํ˜•์„ ์ขŒํ‘œ๋กœ ๋ณผ ๊ฒƒ์ธ๊ฐ€?

 

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ํ•˜๋‚˜์˜ ์ขŒํ‘œ๋Š” ๋„“์ด๊ฐ€ 1์ธ ์ •์‚ฌ๊ฐํ˜• ์œผ๋กœ ์ทจ๊ธ‰ํ•˜๋ฏ€๋กœ 2๋ฒˆ ์จฐ ๊ด€์ ์œผ๋กœ ๋ณด๋Š” ๊ฒƒ์ด ๋งค์šฐ ํŽธํ•  ๊ฒƒ์ด๋‹ค.

 

์ •๋‹ต ์ฝ”๋“œ

 

public class Main {

    private static int m;
    private static int n;

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

        String[] mnk = br.readLine().split(" ");

        m = Integer.parseInt(mnk[0]); // y
        n = Integer.parseInt(mnk[1]); // x
        int k = Integer.parseInt(mnk[2]); // ๊ฐœ์ˆ˜

        boolean[][] arr = new boolean[m][n];

        for (int i = 0; i < k; i++) {
            String[] s = br.readLine().split(" ");
            int fromX = Integer.parseInt(s[0]);
            int fromY = Integer.parseInt(s[1]);

            int toX = Integer.parseInt(s[2]);
            int toY = Integer.parseInt(s[3]);

            mark(arr, fromX, fromY, toX, toY); // ์ƒ‰์น 
        }

        ArrayList<Integer> list = new ArrayList<>();

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if(!arr[i][j]) {
                    list.add(getConnectedComponent(arr, j, i));
                }
            }
        }
        Collections.sort(list);

        bw.write(list.size() + "\n");

        for(int value : list) {
            bw.write(value + " ");
        }

        bw.flush();
        bw.close();
    }

    private static void mark(boolean[][] arr, int fromX, int fromY, int toX, int toY) {
        for (int i = fromY; i < toY; i++) {
            for (int j = fromX; j < toX; j++) {
                arr[i][j] = true;
            }
        }
    }

    private static int getConnectedComponent(boolean[][] arr, int x, int y) {

        int[] xPos = {1, -1, 0, 0};
        int[] yPos = {0, 0, 1, -1};

        Queue<Position> queue = new LinkedList<>();
        queue.add(new Position(x, y));
        arr[y][x] = true;

        int count = 1;

        while(!queue.isEmpty()) {
            Position front = queue.remove();

            for (int i = 0; i < 4; i++) {
                int xx = front.x + xPos[i];
                int yy = front.y + yPos[i];

                if(0 <= xx && xx < n && 0 <= yy && yy < m) {
                    if(!arr[yy][xx]) {
                        arr[yy][xx] = true;
                        queue.add(new Position(xx, yy));
                        count++;
                    }
                }
            }
        }

        return count;
    }

    private static class Position {
        int x;
        int y;

        Position(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€