๊ธฐ๋ก๋ฐฉ

BOJ_2636 : ์น˜์ฆˆ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2636 : ์น˜์ฆˆ

Soom_1n 2023. 4. 6. 16:15

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

2636๋ฒˆ: ์น˜์ฆˆ

์•„๋ž˜ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ์นธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ํŒ์ด ์žˆ๊ณ , ๊ทธ ์œ„์— ์–‡์€ ์น˜์ฆˆ(ํšŒ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„)๊ฐ€ ๋†“์—ฌ ์žˆ๋‹ค. ํŒ์˜ ๊ฐ€์žฅ์ž๋ฆฌ(<๊ทธ๋ฆผ 1>์—์„œ ๋„ค๋ชจ ์นธ์— X์นœ ๋ถ€๋ถ„)์—๋Š” ์น˜์ฆˆ๊ฐ€ ๋†“

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • H x W ๊ฒฉ์ž ํŒ์— ์น˜์ฆˆ๊ฐ€ ์žˆ๋‹ค.
    • ์น˜์ฆˆ๋Š” ๊ฐ€์žฅ์ž๋ฆฌ์— ๋†“์ด์ง€ ์•Š๋Š”๋‹ค.
    • ๊ณต๊ธฐ์™€ ์ ‘์ด‰ํ•œ ์น˜์ฆˆ๋Š” ํ•œ ์‹œ๊ฐ„ ๋’ค ๋…น์•„ ์—†์–ด์ง„๋‹ค.
    • ์น˜์ฆˆ ์•ˆ์—๋Š” 1๊ฐœ ์ด์ƒ์˜ ๊ตฌ๋ฉ์ด ์žˆ๋‹ค.
    • ๊ตฌ๋ฉ ์•ˆ์˜ ๊ณต๊ธฐ๋Š” ์™ธ๋ถ€ ๊ณต๊ธฐ์™€ ์ ‘์ด‰ํ•˜๊ธฐ ์ „๊นŒ์ง„ ์น˜์ฆˆ๋ฅผ ๋…น์ด์ง€ ์•Š๋Š”๋‹ค.
    • ์น˜์ฆˆ๊ฐ€ ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง€๋Š” ๋ฐ ๊นŒ์ง€ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„๊ณผ ์ง์ „ ์น˜์ฆˆ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์ด๋ฏ€๋กœ BFSํ˜น์€ DFS๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

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

public class Main {
    private static class Point {
        int r, c;

        public Point(int r, int c) {
            this.r = r;
            this.c = c;
        }
    }

    private static int H, W;
    private static int[][] map;
    private static int[] dx = {-1, 0, 1, 0};
    private static int[] dy = {0, 1, 0, -1};
    private static boolean[][] visited;
    private static Queue<Point> que;

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        H = Integer.parseInt(st.nextToken());
        W = Integer.parseInt(st.nextToken());
        map = new int[H][W];

        int cheese = 0;
        for (int i = 0; i < H; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < W; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if (map[i][j] == 1) cheese++;
            }
        }

        // DFS
        que = new LinkedList<>();
        visited = new boolean[H][W];
        dfs(0, 0);

        // BFS
        int cnt = 0, size = 0;
        while (!que.isEmpty()) {
            cheese -= size;
            size = que.size();
            cnt++;
            for (int i = 0; i < size; i++) {
                Point point = que.poll();
                int r = point.r;
                int c = point.c;
                map[r][c] = 2;
                for (int j = 0; j < dx.length; j++) {
                    int x = point.r + dx[j];
                    int y = point.c + dy[j];
                    if (0 <= x && x < H && 0 <= y && y < W) {
                        if (map[x][y] == 1 && !visited[x][y]) {
                            visited[x][y] = true;
                            que.add(new Point(x, y));
                        }
                        else if(map[x][y] == 0)
                            dfs(x, y);
                    }
                }
            }
        }

        // Output
        System.out.println(cnt);
        System.out.println(cheese);
    }

    private static void dfs(int r, int c) {
        map[r][c] = 2;
        for (int i = 0; i < dx.length; i++) {
            int x = r + dx[i];
            int y = c + dy[i];

            if (0 <= x && x < H && 0 <= y && y < W) {
                if (map[x][y] == 1 && !visited[x][y]) {
                    visited[x][y] = true;
                    que.add(new Point(x, y));
                } else if (map[x][y] == 0) {
                    dfs(x, y);
                }
            }
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋…น๊ธฐ ์ง์ „ ์น˜์ฆˆ๋Š” ํ์— ๋‹ด๋Š”๋‹ค.
    • ์ฒ˜์Œ ์™ธ๋ถ€ ๊ณต๊ธฐ์™€ ๋งž๋‹ฟ์€ ์น˜์ฆˆ๋ฅผ ๋…น์—ฌ์•ผ ํ•œ๋‹ค.
    • dfs๋กœ 0,0 ๋ถ€ํ„ฐ ๋งž๋‹ฟ์€ ๊ณต๊ธฐ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•ด์„œ, ๊ทธ ์ฃผ๋ณ€์˜ ์น˜์ฆˆ๋ฅผ ํ์— ๋‹ด๋Š”๋‹ค.
    • ํ™•์ธํ•œ ๊ณต๊ธฐ๋Š” ๋”์ด์ƒ ์น˜์ฆˆ๋ฅผ ๋…น์ผ ์ผ์ด ์—†์œผ๋ฏ€๋กœ 2๋ฅผ ์ €์žฅํ•ด๋‘”๋‹ค.
  • ํ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • ํ ์‚ฌ์ด์ฆˆ ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋…น์ผ ์น˜์ฆˆ๋ฅผ ๊บผ๋‚ธ๋‹ค.
    • ๋…น์€ ์น˜์ฆˆ ์ž๋ฆฌ์—๋„ 2๋ฅผ ์ €์žฅํ•œ๋‹ค.
    • ์ฃผ๋ณ€์— ์น˜์ฆˆ๊ฐ€ ์žˆ์œผ๋ฉด ๋‹ค์Œ์— ๋…น์ผ ๊ฒƒ์ด๋ฏ€๋กœ ํ์— ๋‹ด๋Š”๋‹ค.
    • ์ฃผ๋ณ€์— ๊ณต๊ธฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ์น˜์ฆˆ๋ฅผ ๋…น์ผ ์ƒํƒœ๊ฐ€ ๋œ ๊ฒƒ์ด๋ฏ€๋กœ dfs๋กœ ์ธ์ ‘ํ•œ ๊ณต๊ธฐ๋ฅผ ๋ชจ๋‘ ์ฐพ์•„ ๋…น์ผ ์น˜์ฆˆ๋ฅผ ํ์— ๋‹ด๋Š”๋‹ค.
  • ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•œ ํ›„ ๊ฒฝ๊ณผ ์‹œ๊ฐ„๊ณผ ์ง์ „ ์น˜์ฆˆ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • ์น˜์ฆˆ ๊ฐœ์ˆ˜๋Š” ์ฒ˜์Œ ์ž…๋ ฅ ์‹œ ์นด์šดํŠธํ–ˆ๋‹ค๊ฐ€, ํ์—์„œ ๋…น์ผ ์น˜์ฆˆ๋ฅผ ๊บผ๋‚ผ ๋•Œ ๋งˆ๋‹ค ์ฐจ๊ฐํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • BFS์™€ DFS๋ฅผ ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•ด์„œ ํ’€์ดํ–ˆ์ง€๋งŒ, BFS๋กœ๋งŒ ํ†ต์ผํ•ด์„œ ํ’€์—ˆ์–ด๋„ ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™๋‹ค.

728x90