2 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 1


๐Ÿ“Œ ๋ฌธ์ œ

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

image

image


๐Ÿ“Œ ํ’€์ด

์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ ๊ฐ€์žฅ ์–ด๋ ต๋‹ค๋Š” BFS๊ด€๋ จ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•œ ๋‹ค์Œ ์—ฐ๊ด€๋œ ๋ฌธ์ œ๋ฅผ ์ฐพ์•„๋ณด์•˜๋‹ค.
๊ทผ๋ฐ BFS ๋ฌธ์ œ๋Š” ๋‚œ์ด๋„๊ฐ€ ๋ธŒ๋ก ์ฆˆ๋Š” ์ปค๋…• ์‹ค๋ฒ„2๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ๋‹ค.
๋–จ๋ฆฌ๋Š” ๋งˆ์Œ์œผ๋กœ ์ฒ˜์Œ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜๋‹ค.

์ฒ˜์Œ์—๋Š” boolean[][] drawPaper๊ฐ€ ์•„๋‹Œ class paper๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ int isCheck์™€ int isDraw๋ฅผ ๋„ฃ์–ด์„œ ๊ตฌ๋ณ„์„ ํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๊ตณ์ด ๋‘˜์„ ๊ตฌ๋ณ„ํ•  ํ•„์š”์—†์ด, ํ™•์ธํ•œ ๊ณณ์€ ๋นˆ๊ณณ์œผ๋กœ ๋ฐ”๊ฟ”๋ฒ„๋ ค์„œ ์ฒ˜๋ฆฌํ•ด๋ฒ„๋ฆฌ๋ฉด ์šฉ๋Ÿ‰๋„ ์•„๋ผ๊ณ , ์†๋„๋„ ๋นจ๋ผ์งˆ ๊ฒƒ ๊ฐ™์•„์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ์„ ์„ ํ•˜์˜€๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์—ด์˜ ๋ชจ๋“ ๊ณณ์— ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ ธ ์žˆ๋Š”์ง€(true) ํ™•์ธ์„ ํ•˜๋ฉด์„œ, ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ ธ ์žˆ์œผ๋ฉด(true), ๊ทธ๊ณณ์˜ ๊ทธ๋ฆผ์„ ์ง€์šดํ›„(false), Queue์— ์œ„์น˜๋ฅผ ์ €์žฅํ•˜๊ณ , currnetArea๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , paperCount์— 1์„ ๋”ํ•ด์ฃผ์—ˆ๋‹ค. ์ดํ›„ while(!q.isEmpty())๋ฌธ์„ ํ†ตํ•ด ๊ทธ๋ฆผ์ด ์žˆ๋˜ ์œ„์น˜์— ์ ‘ํ•ด์žˆ๋Š” ์ƒํ•˜์ขŒ์šฐ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•˜์—ฌ, DR/DC๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ์œ„์น˜๋ฅผ ๊ตฌํ•ด์ฃผ์—ˆ๋‹ค.
์ดํ›„ ๊ทธ ์œ„์น˜์— ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ ธ(true)์žˆ์œผ๋ฉด ๋นˆ๊ณณ(false)์œผ๋กœ ๋ฐ”๊พธ๊ณ , ๊ทธ ์œ„์น˜๋ฅผ ๋˜๋‹ค์‹œ Queue์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
์ดํ›„ whlie๋ฌธ์ด ๋๋‚˜๋ฉด Math.max๋ฅผ ํ†ตํ•˜์—ฌ currentArea์™€ maxArea๋ฅผ ๋น„๊ตํ•˜์˜€๋‹ค.

์ฒ˜์Œ์œผ๋กœ ํ‘ผ BFS ๋ฌธ์ œ์˜€์ง€๋งŒ, ์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ๊ฒŒ ํ’€๋ ค์„œ ๊ธฐ๋ถ„์ด ์ข‹์•˜๋‹ค.


๐Ÿ“Œ Code

package BFS;

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

public class N1926 {
    static boolean[][] drawPaper;

    static final int[] DR = {0, -1, 0, 1};
    static final int[] DC = {1, 0, -1, 0};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        drawPaper = new boolean[n][m];

        Queue<Integer[]> q = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < m; j++) {
                if (Integer.parseInt(st.nextToken()) == 1) {
                    drawPaper[i][j] = true;
                } else {
                    drawPaper[i][j] = false;
                }
            }
        }
        int maxArea = 0;
        int currentArea = 0;
        int paperCount = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (drawPaper[i][j]) {
                    drawPaper[i][j] = false;
                    q.add(new Integer[]{i, j});
                    currentArea = 1;
                    paperCount += 1;
                }
                while (!q.isEmpty()) {
                    Integer[] cur = q.poll();
                    for (int k = 0; k < 4; k++) {
                        int nextR = cur[0] + DR[k];
                        int nextC = cur[1] + DC[k];
                        if (nextR < 0 || nextR >= n || nextC < 0 || nextC >= m ||
                                !drawPaper[nextR][nextC]) {
                            continue;
                        }
                        drawPaper[nextR][nextC] = false;
                        q.add(new Integer[]{nextR, nextC});
                        currentArea += 1;
                    }
                }
                maxArea = Math.max(currentArea, maxArea);
            }
        }
        System.out.println(paperCount + " " + maxArea);
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜