๊ธฐ๋ก๋ฐฉ

BOJ_17144 : ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•! ๋ณธ๋ฌธ

CodingTest/Java

BOJ_17144 : ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•!

Soom_1n 2023. 4. 7. 14:23

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

 

17144๋ฒˆ: ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•!

๋ฏธ์„ธ๋จผ์ง€๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์‚ฌ๊ณผ๋Š” ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๋ฅผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ์„ฑ๋Šฅ์„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์‚ฌ๊ณผ๋Š” ์ง‘์„ ํฌ๊ธฐ๊ฐ€ R×C์ธ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋ƒˆ๊ณ , 1×1 ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆด๋‹ค. ๊ตฌ์‚ฌ

www.acmicpc.net



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

  • ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๋กœ ์ธํ•œ ์œ„์ชฝ ๊ณต๊ธฐ ์ˆœํ™˜๊ณผ ์•„๋ž˜์ชฝ ๊ณต๊ธฐ ์ˆœํ™˜์ด ์ผ์–ด๋‚œ๋‹ค.
    • ๋จผ์ง€๋Š” ๋งค ์ดˆ ํ™•์‚ฐํ•œ๋‹ค.
    • T์ดˆ ํ›„ ๋ฐฉ์— ๋‚จ์•„์žˆ๋Š” ๋ฏธ์„ธ๋จผ์ง€์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

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

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

public class Main {
    private static int R, C, top, bottom;
    private static int[][] room;
    private static int[] dx = {-1, 0, 1, 0}; // ์ƒ ์ขŒ ํ•˜ ์šฐ
    private static int[] dy = {0, 1, 0, -1};
    private static void dust() {        // ๋จผ์ง€ ํ™•์‚ฐ
        int[][] temp = new int[R][C];   // ๋จผ์ง€ ๋ˆ„์ 

        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {

                if (room[i][j] > 0) { // ํ™•์‚ฐํ•  ๋จผ์ง€๊ฐ€ ์žˆ๋‹ค๋ฉด
                    int cnt = 0; // ํ™•์‚ฐ ์ˆ˜
                    for (int k = 0; k < 4; k++) {
                        int x = i + dx[k];
                        int y = j + dy[k];
                        // ๋ฐฉ ๋ฒ”์œ„ ์•ˆ์— ์žˆ์œผ๋ฉฐ, ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์™€ ์•ˆ๊ฒน์น˜๋ฉด ๋จผ์ง€ ํ™•์‚ฐ
                        if (0 <= x && x < R && 0 <= y && y < C && !(y==0&&(x==top||x==bottom))) {
                            cnt++;
                            temp[x][y] += room[i][j]/5;
                        }
                    }
                    temp[i][j] += room[i][j] - (room[i][j]/5)*cnt;
                }
            }
        }
        room = temp;
    }

    private static void top_wind() {    // ๋ฐ˜์‹œ๊ณ„ ๋ฐ”๋žŒ ์ˆœํ™˜
        if (top > 0) {
            for (int i = top-2; i >= 0; i--) {
                room[i+1][0] = room[i][0];
            }

            for (int i = 1; i < C; i++) {
                room[0][i-1] = room[0][i];
            }

            for (int i = 1; i <= top; i++) {
                room[i-1][C-1] = room[i][C-1];
            }

            for (int i = C-2; i > 0; i--) {
                room[top][i+1] = room[top][i];
            }
            room[top][1] = 0;
        }
    }

    private static void bottom_wind() { // ์‹œ๊ณ„ ๋ฐ”๋žŒ ์ˆœํ™˜
        if (bottom < R) {
            for (int i = bottom+2; i < R; i++) {
                room[i-1][0] = room[i][0];
            }

            for (int i = 1; i < C; i++) {
                room[R-1][i-1] = room[R-1][i];
            }

            for (int i = R-2; i >= bottom; i--) {
                room[i+1][C-1] = room[i][C-1];
            }

            for (int i = C-2; i > 0; i--) {
                room[bottom][i+1] = room[bottom][i];
            }
            room[bottom][1] = 0;
        }
    }

    private static int sum_Dust() {
        int sum = 0;
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if (room[i][j] > 0) {
                    sum += room[i][j];
                }
            }
        }
        return sum;
    }

    private static void print() {
        System.out.println("============================");
        room[top][0] = -1;
        room[bottom][0] = -1;
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                System.out.print(room[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        int T = Integer.parseInt(st.nextToken());
        room = new int[R][C];
        top = -1; bottom = -1;
        for (int i = 0; i < R; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < C; j++) {
                room[i][j] = Integer.parseInt(st.nextToken());
                if (room[i][j] == -1) {
                    if (top == -1) top = i;
                    else bottom = i;
                }
            }
        }

        // ์‹œ๊ฐ„ ์ง„ํ–‰
        for (int t = 0; t < T; t++) {
//            print();
            dust(); // ๋จผ์ง€ ํ™•์‚ฐ
            top_wind();     // ๋ฐ˜์‹œ๊ณ„ ๋ฐ”๋žŒ ์ˆœํ™˜
            bottom_wind();  // ์‹œ๊ณ„ ๋ฐ”๋žŒ ์ˆœํ™˜
//            print();
        }

        // Output
        System.out.println(sum_Dust());
    }
}

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

  • T์ดˆ ๋™์•ˆ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • ๋จผ์ง€๊ฐ€ ํ™•์‚ฐ๋œ๋‹ค
      • ์ธ์ ‘ 4์นธ์œผ๋กœ ํ™•์‚ฐ๋˜๋Š”๋ฐ, temp ๋ฐฐ์—ด์— ํ™•์‚ฐ๋œ ๋จผ์ง€๋ฅผ ๋ˆ„์ ํ•œ๋‹ค.
      • ์›๋ณธ  ๋ฐฉ ์ƒํƒœ์ธ room ๋ฐฐ์—ด์˜ ๊ฐ’์„ temp๋ฐฐ์—ด์˜ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
    • ์œ„์ชฝ์€ ๋ฐ˜์‹œ๊ณ„, ์•„๋ž˜์ชฝ์€ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ”๋žŒ์ด ์ˆœํ™˜ํ•œ๋‹ค.
      • ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ๋†’์ด์— ๋”ฐ๋ผ์„œ ์ˆœํ™˜ ์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค.
  • ๋‚จ์€ ๋จผ์ง€ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ฐ”๋žŒ ์ˆœํ™˜ ๋ถ€๋ถ„์ด ๊ตฌํ˜„ํ•˜๊ธฐ์— ํ—ท๊น”๋ฆด ๋ถ€๋ถ„์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™์•„์„œ, ๋”ฐ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๋นผ์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_17070 : ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ 1  (0) 2023.04.07
BOJ_14889 : ์Šคํƒ€ํŠธ์™€ ๋งํฌ  (0) 2023.04.07
BOJ_1068 : ํŠธ๋ฆฌ  (0) 2023.04.07
BOJ_13460 : ๊ตฌ์Šฌ ํƒˆ์ถœ 2  (0) 2023.04.07
BOJ_16236 : ์•„๊ธฐ ์ƒ์–ด  (0) 2023.04.07