๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

 

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

๋นก๊ตฌํ˜„ ๋ฌธ์ œ๋Š” ์š”๊ตฌ์‚ฌํ•ญ ๋ณ„๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์ž˜ ๋‚˜๋ˆ  ๊ตฌํ˜„ํ•˜๋Š” ๊ฒŒ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ํ•ด๋„ ์ค‘๊ฐ„์— ๋กœ์ง์ด ์ž˜๋ชป๋˜๋ฉด ๋””๋ฒ„๊น…ํ•˜๊ธฐ๊ฐ€ ์ •๋ง ์–ด๋ ต๋„ค์š”. ์ €๋Š” ํŒŒ์ด์–ด๋ณผ์ด ๊ฐ™์€ ์œ„์น˜์— ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•œ 2์ฐจ์› Queue ๋ฐฐ์—ด, ํ˜„์žฌ ๋งต์— ์กด์žฌํ•˜๋Š” ํŒŒ์ด์–ด๋ณผ๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” Queue ์ด๋ ‡๊ฒŒ 2๊ฐœ๋ฅผ ์จ์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ํ์—์„œ๋Š” ํŒŒ์ด์–ด๋ณผ ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜๊ตฌ์š”.

class FireBall {
    FireBall(int row, int column, int mass, int velocity, int direction) {
        this.row = row;
        this.column = column;
        this.mass = mass;
        this.velocity = velocity;
        this.direction = direction;
    }

    int row, column, mass, velocity, direction;
}

 

BufferedReader๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„์ฃผ๊ณ , 2์ฐจ์› ํ ๋ฐฐ์—ด๊ณผ ๊ด€๋ฆฌ ํ๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ–‰, ์—ด, ์งˆ๋Ÿ‰, ์†๋„, ๋ฐฉํ–ฅ ์ •๋ณด๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•„ ๊ด€๋ฆฌ ํ์—๋‹ค๊ฐ€ ๋„ฃ์–ด์ฃผ์—ˆ์ฃ .

private static void initialize() throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] args = br.readLine().split(" ");

    N = Integer.parseInt(args[0]);
    M = Integer.parseInt(args[1]);
    K = Integer.parseInt(args[2]);
    Field = new LinkedList[N][N];
    FireBalls = new LinkedList<>();

    // 1. 2์ฐจ์› ๋ฐฐ์—ด ๊ฐ ์š”์†Œ๋งˆ๋‹ค ํ(Queue) ์ƒ์„ฑ
    for (int r = 0; r < N; r++) {
        for (int c = 0; c < N; c++) {
            Field[r][c] = new LinkedList<FireBall>();
        }
    }

    // 2. ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ํŒŒ์ด์–ด๋ณผ ์ •๋ณด๋ฅผ ๋‹ด๋Š” FireBalls ํ์— ์ดˆ๊ธฐ๊ฐ’ ๋„ฃ๊ธฐ
    for (int i = 0; i < M; i++) {
        args = br.readLine().split(" ");
        int row = Integer.parseInt(args[0]) - 1;
        int column = Integer.parseInt(args[1]) - 1;
        int mass = Integer.parseInt(args[2]);
        int velocity = Integer.parseInt(args[3]);
        int direction = Integer.parseInt(args[4]);

        FireBalls.add(new FireBall(row, column, mass, velocity, direction));
    }

    br.close();
}

 

์ž…๋ ฅ๊ฐ’ ๋ฐ›๊ธฐ๋ฅผ ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด ํŒŒ์ด์–ด๋ณผ์„ ์ด๋™์‹œํ‚ค๋Š” moveFireBalls()์™€ ๋ชจ๋‘ ์ด๋™ํ•œ ํŒŒ์ด์–ด๋ณผ๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” processFireBalls()๋ฅผ K๋ฒˆ ๋ฐ˜๋ณตํ•  ๊ฒ๋‹ˆ๋‹ค.

 

moveFireBalls()

ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ํŒŒ์ด์–ด๋ณผ์˜ ๊ฐœ์ˆ˜(FireBalls.size())๋งŒํผ for ๋ฌธ์„ ๋Œ์•„ ๋‹ค์Œ ์œ„์น˜๋กœ ์ด๋™์‹œ์ผœ ์ค„ ๊ฒ๋‹ˆ๋‹ค. ์ด๋•Œ, ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋งต์€ ๋ฌดํ•œ ๋งต์ด๋ฏ€๋กœ ๋งต ๋ฐ–์„ ๋„˜์–ด๊ฐ€๋„ ๋‹ค์‹œ ๋ฐ˜๋Œ€ํŽธ์œผ๋กœ ๋Œ์•„์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ ์šฉํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ(%)์„ ์ ์šฉํ–ˆ์œผ๋ฉฐ, ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ์ ์šฉํ•ด๋„ ๊ทธ๋Œ€๋กœ ์Œ์ˆ˜์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ N๋งŒํผ ์ถ”๊ฐ€๋กœ ๋”ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

private static void moveFireBalls() {
    int size = FireBalls.size();

    for (int i = 0; i < size; i++) {
        FireBall fireBall = FireBalls.poll();
        int nextRow = (fireBall.row + RowDirections[fireBall.direction] * fireBall.velocity) % N;
        int nextColumn = (fireBall.column + ColumnDirections[fireBall.direction] * fireBall.velocity) % N;

        if (nextRow < 0)    nextRow += N;
        if (nextColumn < 0) nextColumn += N;

        fireBall.row = nextRow;
        fireBall.column = nextColumn;
        Field[nextRow][nextColumn].add(fireBall);
    }
}

 

processFireBalls()

์ „์ฒด ๋งต์„ ์ˆœํšŒํ•˜๋ฉด์„œ ํŒŒ์ด์–ด๋ณผ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์—ฐ์‚ฐ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • ํŒŒ์ด์–ด๋ณผ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ์ธ ๊ฒฝ์šฐ : ๋งต์˜ ํ˜„์žฌ ์œ„์น˜์—์„œ ํŒŒ์ด์–ด๋ณผ์„ ๋นผ๋‚ด์–ด ๊ด€๋ฆฌ ํ(FireBalls)์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
  • ํŒŒ์ด์–ด๋ณผ์˜ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ : ํŒŒ์ด์–ด๋ณผ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ 4๊ฐœ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ณผ์ • ์ง„ํ–‰ → splitFireBalls()
private static void processFireBalls() {
    for (int row = 0; row < N; row++) {
        for (int column = 0; column < N; column++) {
            // ๊ฐ™์€ ๊ณณ์— ํŒŒ์ด์–ด๋ณผ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ์ธ ๊ฒฝ์šฐ -> ๊ด€๋ฆฌ ํ์— ๋„ฃ๊ธฐ
            if (Field[row][column].size() == 1) {
                FireBalls.add(Field[row][column].poll());
            }

            // ๊ฐ™์€ ๊ณณ์— ํŒŒ์ด์–ด๋ณผ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ -> 4๊ฐœ๋กœ ๋ถ„๋ฆฌ
            else if (Field[row][column].size() > 1) {
                splitFireBalls(row, column);
            }
        }
    }
}

 

splitFireBalls() ๋ฉ”์„œ๋“œ๋Š” ๊ฐ™์€ ๊ณณ์— ์žˆ๋Š” 2๊ฐœ ์ด์ƒ์˜ ํŒŒ์ด์–ด๋ณผ๋“ค์ด ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ ธ 4๊ฐœ๋กœ ๋ถ„๋ฆฌ๋˜๋Š” ์š”๊ตฌ์‚ฌํ•ญ์„ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

private static void splitFireBalls(int row, int column) {
    int sumMass = 0, sumVelocity = 0, size = Field[row][column].size();
    boolean isAllEven = true, isAllOdd = true;

    while (!Field[row][column].isEmpty()) {
        FireBall fireBall = Field[row][column].poll();
        sumMass += fireBall.mass;
        sumVelocity += fireBall.velocity;

        if (fireBall.direction % 2 == 0)    isAllOdd = false;
        else                                isAllEven = false;
    }

    int newMass = sumMass / 5;
    int newVelocity = sumVelocity / size;

    if (newMass > 0) {
        for (int i = 0; i < 4; i++) {
            int newDirection = (isAllEven || isAllOdd) ? i * 2 : i * 2 + 1;
            FireBalls.add(new FireBall(row, column, newMass, newVelocity, newDirection));
        }
    }
}

 

์ด๋Ÿฌํ•œ ๊ณผ์ •์„ K๋ฒˆ ๋ฐ˜๋ณตํ•œ ํ›„, ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ํŒŒ์ด์–ด๋ณผ๋“ค์˜ ์งˆ๋Ÿ‰ ํ•ฉ์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ์ •๋‹ต์ž…๋‹ˆ๋‹ค.

private static int getFireBallMass() {
    int result = 0;

    for (FireBall fireBall : FireBalls) {
        result += fireBall.mass;
    }

    return result;
}

 

์ „์ฒด ์†Œ์Šค์ฝ”๋“œ๋Š” ์•„๋ž˜์— ์žˆ์Šต๋‹ˆ๋‹ค.

๋”๋ณด๊ธฐ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Queue;
import java.util.LinkedList;

class FireBall {
    FireBall(int row, int column, int mass, int velocity, int direction) {
        this.row = row;
        this.column = column;
        this.mass = mass;
        this.velocity = velocity;
        this.direction = direction;
    }

    int row, column, mass, velocity, direction;
}

public class BOJ20056 {
    private static int N, M, K;
    private static int[] RowDirections = { -1, -1, 0, 1, 1, 1, 0, -1 };
    private static int[] ColumnDirections = { 0, 1, 1, 1, 0, -1, -1, -1 };
    private static Queue<FireBall>[][] Field;
    private static Queue<FireBall> FireBalls;

    public static void main(String[] args) throws Exception {
        initialize();

        for (int i = 0; i < K; i++) {
            moveFireBalls();
            processFireBalls();
        }

        int answer = getFireBallMass();
        System.out.println(answer);
    }

    private static void moveFireBalls() {
        int size = FireBalls.size();

        for (int i = 0; i < size; i++) {
            FireBall fireBall = FireBalls.poll();
            int nextRow = (fireBall.row + RowDirections[fireBall.direction] * fireBall.velocity) % N;
            int nextColumn = (fireBall.column + ColumnDirections[fireBall.direction] * fireBall.velocity) % N;

            if (nextRow < 0)    nextRow += N;
            if (nextColumn < 0) nextColumn += N;

            fireBall.row = nextRow;
            fireBall.column = nextColumn;
            Field[nextRow][nextColumn].add(fireBall);
        }
    }

    private static void processFireBalls() {
        for (int row = 0; row < N; row++) {
            for (int column = 0; column < N; column++) {
                // ๊ฐ™์€ ๊ณณ์— ํŒŒ์ด์–ด๋ณผ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ -> ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ํŒŒ์ด์–ด๋ณผ์„ ๊ด€๋ฆฌํ•˜๋Š” ํ(FireBalls)์— ๋„ฃ์–ด์ฃผ๊ธฐ
                if (Field[row][column].size() == 1) {
                    FireBalls.add(Field[row][column].poll());
                }

                // ๊ฐ™์€ ๊ณณ์— ํŒŒ์ด์–ด๋ณผ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ -> 4๊ฐœ๋กœ ๋ถ„๋ฆฌ
                else if (Field[row][column].size() > 1) {
                    splitFireBalls(row, column);
                }
            }
        }
    }

    private static void splitFireBalls(int row, int column) {
        int sumMass = 0, sumVelocity = 0, size = Field[row][column].size();
        boolean isAllEven = true, isAllOdd = true;

        while (!Field[row][column].isEmpty()) {
            FireBall fireBall = Field[row][column].poll();
            sumMass += fireBall.mass;
            sumVelocity += fireBall.velocity;

            if (fireBall.direction % 2 == 0)    isAllOdd = false;
            else                                isAllEven = false;
        }

        int newMass = sumMass / 5;
        int newVelocity = sumVelocity / size;

        if (newMass > 0) {
            for (int i = 0; i < 4; i++) {
                int newDirection = (isAllEven || isAllOdd) ? i * 2 : i * 2 + 1;
                FireBalls.add(new FireBall(row, column, newMass, newVelocity, newDirection));
            }
        }
    }

    private static int getFireBallMass() {
        int result = 0;

        for (FireBall fireBall : FireBalls) {
            result += fireBall.mass;
        }

        return result;
    }

    private static void initialize() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] args = br.readLine().split(" ");

        N = Integer.parseInt(args[0]);
        M = Integer.parseInt(args[1]);
        K = Integer.parseInt(args[2]);
        Field = new LinkedList[N][N];
        FireBalls = new LinkedList<>();

        // 1. 2์ฐจ์› ๋ฐฐ์—ด ๊ฐ ์š”์†Œ๋งˆ๋‹ค ํ(Queue) ์ƒ์„ฑ
        for (int r = 0; r < N; r++) {
            for (int c = 0; c < N; c++) {
                Field[r][c] = new LinkedList<FireBall>();
            }
        }

        // 2. ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ํŒŒ์ด์–ด๋ณผ ์ •๋ณด๋ฅผ ๋‹ด๋Š” FireBalls ํ์— ์ดˆ๊ธฐ๊ฐ’ ๋„ฃ๊ธฐ
        for (int i = 0; i < M; i++) {
            args = br.readLine().split(" ");
            int row = Integer.parseInt(args[0]) - 1;
            int column = Integer.parseInt(args[1]) - 1;
            int mass = Integer.parseInt(args[2]);
            int velocity = Integer.parseInt(args[3]);
            int direction = Integer.parseInt(args[4]);

            FireBalls.add(new FireBall(row, column, mass, velocity, direction));
        }

        br.close();
    }
}
728x90
๋ฐ˜์‘ํ˜•