열공스토리

[백준] 23288 - 주사위 굴리기2 (JAVA 객체지향적으로 설계해 보기) 본문

백준

[백준] 23288 - 주사위 굴리기2 (JAVA 객체지향적으로 설계해 보기)

열쩡열쩡열쩡_ 2023. 11. 17. 20:18
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    private static int N;
    private static int M;
    private static final int[] nextN = {0, 1, 0, -1};
    private static final int[] nextM = {1, 0, -1, 0};

    static class Dice {
        private int diceNet[];
        private int n;
        private int m;
        private int direction;

        Dice() {
            this.diceNet = new int[]{1, 3, 4, 5, 2, 6}; // 위, 동, 서, 남, 북, 아래
            this.n = 0;
            this.m = 0;
            this.direction = 0; // 0 - 동, 1 - 남, 2 - 서, 3 - 북
        }

        void move() {
            n += nextN[direction];
            m += nextM[direction];
            diceNetAfterEastMove();
            diceNetAfterWestMove();
            diceNetAfterSouthMove();
            diceNetAfterNorthMove();
        }

        void diceNetAfterEastMove() {
            if (direction == 0) {
                int temp = diceNet[0];
                diceNet[0] = diceNet[2];
                diceNet[2] = diceNet[5];
                diceNet[5] = diceNet[1];
                diceNet[1] = temp;
            }
        }

        void diceNetAfterWestMove() {
            if (direction == 2) {
                int temp = diceNet[0];
                diceNet[0] = diceNet[1];
                diceNet[1] = diceNet[5];
                diceNet[5] = diceNet[2];
                diceNet[2] = temp;
            }
        }

        void diceNetAfterSouthMove() {
            if (direction == 1) {
                int temp = diceNet[0];
                diceNet[0] = diceNet[4];
                diceNet[4] = diceNet[5];
                diceNet[5] = diceNet[3];
                diceNet[3] = temp;
            }
        }

        void diceNetAfterNorthMove() {
            if (direction == 3) {
                int temp = diceNet[0];
                diceNet[0] = diceNet[3];
                diceNet[3] = diceNet[5];
                diceNet[5] = diceNet[4];
                diceNet[4] = temp;
            }
        }

        void decideDirection(ScoreMap scoreMap) {
            rotateClockwise(scoreMap);
            rotateCounterClockwise(scoreMap);
            rotateReserve(scoreMap);
        }

        void rotateClockwise(ScoreMap scoreMap) {
            int number =  diceNet[5];
            if (scoreMap.isHigherThan(number, n, m)) {
                direction = (direction + 1) % 4;
            }
        }

        void rotateCounterClockwise(ScoreMap scoreMap) {
            int number =  diceNet[5];
            if (scoreMap.isLowerThan(number, n, m)) {
                direction = (direction + 3) % 4;
            }
        }

        void rotateReserve(ScoreMap scoreMap) {
            int moveN = n + nextN[direction];
            int moveM = m + nextM[direction];
            if (scoreMap.isPositionOutOfBounds(moveN, moveM)) {
                direction = (direction + 2) % 4;
            }
        }
    }

    static class ScoreMap {
        private final int map[][];

        ScoreMap(int[][] map) {
            this.map = map;
        }

        int getScore(int n, int m) {
            return map[n][m] * getSquare(n, m, new boolean[N][M]);
        }

        int getSquare(int n, int m, boolean[][] visited) {
            visited[n][m] = true;
            int score = 1;
            for (int i = 0; i < 4; i++) {
                int moveN = n + nextN[i];
                int moveM = m + nextM[i];
                if (moveN < 0 || moveM < 0 || moveN >= N || moveM >= M) continue;
                if (map[moveN][moveM] != map[n][m] || visited[moveN][moveM]) continue;
                score += getSquare(moveN, moveM, visited);
            }
            return score;
        }

        boolean isHigherThan(int diceNumber, int n, int m) {
            return diceNumber > map[n][m];
        }

        boolean isLowerThan(int diceNumber, int n, int m) {
            return diceNumber < map[n][m];
        }

        boolean isPositionOutOfBounds(int n, int m) {
            return n < 0 || m < 0 || n >= N || m >= M;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[][] arr = new int[N][M];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        ScoreMap scoreMap = new ScoreMap(arr);
        Dice dice = new Dice();

        int score = 0;
        while (K-- > 0) {
            dice.move();
            score += scoreMap.getScore(dice.n, dice.m);
            dice.decideDirection(scoreMap);
        }
        System.out.println(score);
    }
}