Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-mong3125 #52

Merged
merged 1 commit into from
Jun 27, 2024
Merged

15-mong3125 #52

merged 1 commit into from
Jun 27, 2024

Conversation

mong3125
Copy link
Collaborator

@mong3125 mong3125 commented Apr 11, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํ† ๋งˆํ† 
์ด์ „์— ์Šนํ˜ธ๋‹˜์ด ๋จผ์ € ํ‘ธ์‹  ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ‘ผ ํ’€์ด์™€ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ‘ผ ํ’€์ด ๋‘ ๊ฐœ๋กœ ๋‚˜๋ˆ ์„œ ์˜ฌ๋ ค๋ณผ๊ฒŒ์š”.

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

3์‹œ๊ฐ„์ •๋„ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.
๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ƒˆ๋‹ค๋ฉด ๋” ๊ฐ€๋ณ๊ฒŒ ๊ตฌํ˜„ํ–ˆ์„๊ฒƒ ๊ฐ™์€๋ฐ ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„์€ ์ข€ ์˜ค๋ž˜๊ฑธ๋ ธ๋„ค์š”.

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ €๋„ ์ฒ˜์Œ์— ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•ด์„œ ๋ฐฑ์ค€์— ํ†ต๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ œ ์ฝ”๋“œ๋Š” ์Šนํ˜ธ๋‹˜์˜ curQueue = !curQueue; ์ด ์•„์ด๋””์–ด๋ฅผ ์ƒ๊ฐ๋ชปํ•ด์„œ ์ข€ ๋” ๋น„ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค ์ฝ”๋“œ๋ฅผ ์ฐพ๋‹ค๋ณด๋‹ˆ ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์„œ ์†Œ๊ฐœํ•˜๊ณ ์‹ถ์–ด์„œ ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ• (์Šนํ˜ธ๋‹˜๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.)

์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•๋ณด๋‹จ ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๋ ค๊ณ  ๋‚˜์™€์„œ ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ์Šนํ˜ธ๋‹˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.
#42

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class BOJ7569_ํ† ๋งˆํ†  {

    static int M, N, H;
    static Queue<int[]> ripen = new LinkedList<>();   // ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜
    static Queue<int[]> temp = new LinkedList<>();   // ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜
    static int notRipenTomatos = 0;

    // ๋ฐฉํ–ฅ
    static int[][] directions = {
            {1, 0, 0},
            {-1, 0, 0},
            {0, 1, 0},
            {0, -1, 0},
            {0, 0, 1},
            {0, 0, -1},
    };

    // ๋ฐ•์Šค
    static int[][][] box;

    // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
    static boolean[][][] visited;
    public static void main(String[] args) throws IOException {
        // == ์ž…๋ ฅ == //
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        M = Integer.parseInt(st.nextToken());
        N = Integer.parseInt(st.nextToken());
        H = Integer.parseInt(st.nextToken());
        box = new int[H][N][M];
        visited = new boolean[H][N][M];

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < N; j++) {
                st = new StringTokenizer(br.readLine());
                for (int k = 0; k < M; k++) {
                    box[i][j][k] = Integer.parseInt(st.nextToken());

                    // ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜ ์ €์žฅ
                    if (box[i][j][k] == 1) ripen.add(new int[]{i, j, k});
                    else if (box[i][j][k] == 0) notRipenTomatos++;
                }
            }
        }

        // == ํ’€์ด == //
        int count = 0;

        // ์ƒˆ๋กœ ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ์—†์„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
        while (!ripen.isEmpty()) {
            bfs();
            count++;
        }

        // ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์€๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด -1 ๋ฐ˜ํ™˜
        if (notRipenTomatos > 0) {
            count = 0;
        }

        System.out.println(count - 1);
    }

    public static void bfs() {
        temp.clear();

        while (!ripen.isEmpty()) {
            int[] tomato = ripen.remove();
            if (visited[tomato[0]][tomato[1]][tomato[2]]) continue;

            visited[tomato[0]][tomato[1]][tomato[2]] = true;
            ripe(tomato);
        }

        ripen.addAll(temp);
    }

    public static void ripe(int[] tomato) {
        for (int[] direction : directions) {
            int z = tomato[0] + direction[0];
            int y = tomato[1] + direction[1];
            int x = tomato[2] + direction[2];

            if (isInRange(z, y, x)) {
                if (box[z][y][x] == 0) {
                    box[z][y][x] = 1;
                    notRipenTomatos--;
                    temp.add(new int[]{z, y, x});
                }
            }
        }
    }

    public static boolean isInRange(int z, int y, int x) {
        return (x >= 0 && x < M && y >= 0 && y < N && z >= 0 && z < H);
    }
}

๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

๊ฐ€์žฅ ํ•ต์‹ฌ์ด ๋˜๋Š” ์•„์ด๋””์–ด๋Š” ์ƒˆ๋กญ๊ฒŒ ํ† ๋งˆํ† ๋ฅผ ์ตํž๋•Œ, ์ด์ „ ํ† ๋งˆํ† ๋ณด๋‹ค 1๋งŒํผ ํฐ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‚˜์ค‘์— (๊ฐ€์žฅ ํฐ ์ˆ˜ - 1)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

bfs() {
    while ์ต์€ ํ† ๋งˆํ†  ํ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต{
        tomato = ๊ฐ€์žฅ ์œ„์—์žˆ๋Š” ํ† ๋งˆํ†  ์œ„์น˜
        for 6 ๋ฐฉํ–ฅ ์œ„์น˜๋กœ ๋ฐ˜๋ณต๋ฌธ {
            if ๋ฒ”์œ„ ๋‚ด์— ์žˆ๋‹ค๋ฉด {
                if ์•ˆ ์ต์€ ํ† ๋งˆํ† ๋ผ๋ฉด {
                    ์ด์ „ ํ† ๋งˆํ†  +  1๋กœ ์ €์žฅ
                    result = max(result, ์ด์ „ ํ† ๋งˆํ†  + 1)
                    ์ต์ง€ ์•Š์€ ํ† ๋งˆํ†  ์ˆ˜ -= 1

                    ์ต์€ ํ† ๋งˆํ†  ํ์— ์ถ”๊ฐ€
                }
            }
        }
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class BOJ7569_ํ† ๋งˆํ†  {

    static int M, N, H;
    static int[][][] box;

    static Queue<int[]> ripen = new LinkedList<>();   // ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜
    static int notRipenTomatos = 0;
    static int result = 1;
    static int[][] directions = {
            {1, 0, 0},
            {-1, 0, 0},
            {0, 1, 0},
            {0, -1, 0},
            {0, 0, 1},
            {0, 0, -1},
    };

    public static void main(String[] args) throws IOException {
        // == ์ž…๋ ฅ == //
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        M = Integer.parseInt(st.nextToken());
        N = Integer.parseInt(st.nextToken());
        H = Integer.parseInt(st.nextToken());
        box = new int[H][N][M];

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < N; j++) {
                st = new StringTokenizer(br.readLine());
                for (int k = 0; k < M; k++) {
                    box[i][j][k] = Integer.parseInt(st.nextToken());

                    // ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜ ์ €์žฅ
                    if (box[i][j][k] == 1) ripen.add(new int[]{i, j, k});
                    else if (box[i][j][k] == 0) notRipenTomatos++;
                }
            }
        }

        // == ํ’€์ด == //
        bfs();

        // ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์€๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด -1 ๋ฐ˜ํ™˜
        if (notRipenTomatos > 0) {
            result = 0;
        }

        // == ์ถœ๋ ฅ == //
        System.out.println(result - 1);
    }

    public static void bfs() {
        while (!ripen.isEmpty()) {
            int[] tomato = ripen.remove();
            for (int[] direction : directions) {
                int z = tomato[0] + direction[0];
                int y = tomato[1] + direction[1];
                int x = tomato[2] + direction[2];

                if (isInRange(z, y, x)) {
                    if (box[z][y][x] == 0) {
                        box[z][y][x] = box[tomato[0]][tomato[1]][tomato[2]] + 1;
                        result = Math.max(result, box[z][y][x]);
                        notRipenTomatos--;

                        ripen.add(new int[]{z, y, x});
                    }
                }
            }
        }
    }

    public static boolean isInRange(int z, int y, int x) {
        return (x >= 0 && x < M && y >= 0 && y < N && z >= 0 && z < H);
    }
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

  • BFS์—์„œ ์‹œํ–‰ํšŸ์ˆ˜๋ฅผ ์ฐพ์„๋• ๋ฐฐ์—ด์— ์ ์  ์ˆซ์ž๋ฅผ ํ‚ค์›Œ๊ฐ€๋ฉด์„œ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•œ ๋’ค, ๋งˆ์ง€๋ง‰์— ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์ฐพ์Œ์œผ๋กœ์จ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ† ๋งˆํ†  ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๊ทธ ํ›„์— ์ƒ๊ฐ๋งŒ ํ–ˆ์—ˆ๋˜ ๋ฐฉ์‹์„ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋„ค์š”. ๊ฐœ์ธ์ ์œผ๋กœ ๋‘ ๋ฒˆ์งธ ํ’€์ด๊ฐ€ ๋” ์ฝ๊ธฐ ์‰ฌ์šด ์ง๊ด€์ ์ธ BFS ํ’€์ด๋ผ ๋Š๊ปด์ง‘๋‹ˆ๋‹ค. ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants