[백준/14499] 주사위 굴리기(골드 4) JAVA

jkky98·2025년 1월 6일
0

CodingTraining

목록 보기
62/62


풀이 도움 그림판
풀이

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

public class DICE14499 {

    static BufferedReader br;
    static StringTokenizer st;
    static int N;
    static int M;
    static int x;
    static int y;
    static int K;
    static int[][] map;

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

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        x = Integer.parseInt(st.nextToken());
        y = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());

        map = new int[N][M];

        initMap();
        Dice dice = new Dice(x, y, N, M, map);

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < K; i++) {
            int order = Integer.parseInt(st.nextToken());
            dice.move(order);
        }
    }

    private static void initMap() throws IOException {
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
    }

    static class Dice {
        int top = 0;
        int bottom = 0;
        int north = 0;
        int south = 0;
        int east = 0;
        int west = 0;
        int xMin = 0;
        int yMin = 0;

        int x;
        int y;
        int xMax; //N
        int yMax; //M
        int[][] map;


        public Dice(int x, int y, int xMax, int yMax, int[][] map) {
            this.x = x;
            this.y = y;
            this.xMax = xMax - 1;
            this.yMax = yMax - 1;
            this.map = map;
        }

        public void move(int order) {
            if (checkOrder(order)) {
                // 움직임
                switch (order) {
                    case 1:
                        // 주사위 굴리기
                        int bottomPrev1 = bottom;
                        bottom = east;
                        east = top;
                        top = west;
                        west = bottomPrev1;
                        // 주사위 맵 위치 갱신
                        y += 1;
                        // 주사위 바텀값 <-> 맵값 갱신
                        updateMapDice();
                        // 출력
                        System.out.println(top);
                        break;
                    case 2:
                        int bottomPrev2 = bottom;
                        bottom = west;
                        west = top;
                        top = east;
                        east = bottomPrev2;
                        y -= 1;
                        updateMapDice();
                        System.out.println(top);
                        break;
                    case 3:
                        int bottomPrev3 = bottom;
                        bottom = north;
                        north = top;
                        top = south;
                        south = bottomPrev3;
                        x -= 1;
                        updateMapDice();
                        System.out.println(top);
                        break;
                    case 4:
                        int bottomPrev4 = bottom;
                        bottom = south;
                        south = top;
                        top = north;
                        north = bottomPrev4;
                        x += 1;
                        updateMapDice();
                        System.out.println(top);
                        break;
                }

            }
        }

        private boolean checkOrder(int order) {
            switch (order) {
                case 1:
                    if (y + 1 > yMax) {
                        return false;
                    }
                    return true;
                case 2:
                    if (y - 1 < 0) {
                        return false;
                    }
                    return true;
                case 3:
                    if (x - 1 < 0) {
                        return false;
                    }
                    return true;
                case 4:
                    if (x + 1 > xMax) {
                        return false;
                    }
                    return true;
            }
            return false;
        }

        private void updateMapDice() {
            if (map[x][y] == 0) {
                map[x][y] = bottom;
            } else {
                bottom = map[x][y];
                map[x][y] = 0;
            }
        }
    }
}

시키는대로 따라가면 편한 문제 코드의 깔끔함을 위해 if문보다는 switch문을 사용해보았다. switch문을 잘 쓰지 않다보니 switch문의 변수 스코프에 대해 당황했으나 모두 다른 bottomPrev"N"으로 처리했다.

알고리즘이 크게 들어가지 않는 단순 구현 시뮬 문제라 문제 이해 후에는 흐름을 쭉 타고 코딩했다.

문제를 이해하고 코딩에서 걸린시간은 오래지 않지만 백준 문제집에서 항상 불만인 것은 제시문이 이해가 너무 힘들다는 것이다.

주사위는 지도 위에 윗 면이 1이고, 동쪽을 바라보는 방향이 3인 상태로 놓여져 있으며, 놓여져 있는 곳의 좌표는 (x, y) 이다. 가장 처음에 주사위에는 모든 면에 0이 적혀져 있다.

윗 표현도 위의 예시 사진과 설명이 연결되었다는 언급이 필요해보이고,

둘째 줄부터 N개의 줄에 지도에 쓰여 있는 수가 북쪽부터 남쪽으로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다.

이 부분도 말로만 들으면 참 이해가 힘들다.

profile
자바집사의 거북이 수련법

0개의 댓글

관련 채용 정보