백준 14499 주사위 굴리기 (Java,자바)

jonghyukLee·2021년 9월 7일
0

이번에 풀어본 문제는
백준 14499번 주사위 굴리기 입니다.

📕 문제 링크

❗️코드

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

class Dice
{
    int x,y,top,bottom,left,right,front,back;

    public Dice(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public void setBottom(int bottom) {
        this.bottom = bottom;
    }

    public void setLeft(int left) {
        this.left = left;
    }

    public void setRight(int right) {
        this.right = right;
    }

    public void setFront(int front) {
        this.front = front;
    }

    public void setBack(int back) {
        this.back = back;
    }

    public void setTop(int top) {
        this.top = top;
    }
}
public class Main {
    static int n,m;
    static int [][] map;
    static Dice dice;
    static int [] dx = {0,0,-1,1}; // 동 서 북 남
    static int [] dy = {1,-1,0,0};
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        int startX = Integer.parseInt(st.nextToken());
        int startY = Integer.parseInt(st.nextToken());
        int t = Integer.parseInt(st.nextToken());
        dice = new Dice(startX,startY);

        map = new int[n][m];

        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());
            }
        }

        st = new StringTokenizer(br.readLine()); // 이동명령

        StringBuilder sb = new StringBuilder();
        while(st.hasMoreTokens())
        {
            int next = Integer.parseInt(st.nextToken())-1;

            int curX = dice.x, curY = dice.y; // 현위치
            int mx = curX + dx[next], my = curY + dy[next];

            if(!isValid(mx,my)) continue;
            dice.x = mx;
            dice.y = my;
            roll(next);

            if(map[mx][my] == 0)
            {
                map[mx][my] = dice.bottom;
            }
            else
            {
                dice.setBottom(map[mx][my]);
                map[mx][my] = 0;
            }
            sb.append(dice.top).append("\n");
        }
        System.out.print(sb.toString());
    }
    static void roll(int direc)
    {
        switch(direc)
        {
            case 0:
            {
                int tmpTop = dice.top,tmpBottom = dice.bottom, tmpLeft = dice.left, tmpRight = dice.right;
                dice.setTop(tmpRight);
                dice.setLeft(tmpTop);
                dice.setRight(tmpBottom);
                dice.setBottom(tmpLeft);
                break;
            }
            case 1:
            {
                int tmpTop = dice.top,tmpBottom = dice.bottom, tmpLeft = dice.left, tmpRight = dice.right;
                dice.setTop(tmpLeft);
                dice.setLeft(tmpBottom);
                dice.setRight(tmpTop);
                dice.setBottom(tmpRight);
                break;
            }
            case 2:
            {
                int tmpTop = dice.top,tmpBottom = dice.bottom,tmpFront = dice.front, tmpBack = dice.back;
                dice.setTop(tmpFront);
                dice.setFront(tmpBottom);
                dice.setBack(tmpTop);
                dice.setBottom(tmpBack);
                break;
            }
            case 3:
            {
                int tmpTop = dice.top,tmpBottom = dice.bottom,tmpFront = dice.front, tmpBack = dice.back;
                dice.setTop(tmpBack);
                dice.setFront(tmpTop);
                dice.setBack(tmpBottom);
                dice.setBottom(tmpFront);
                break;
            }
        }
    }
    static boolean isValid(int x, int y)
    {
        return x >= 0 && y >= 0 && x < n && y < m;
    }
}

📝 풀이

지도 위에서 주사위를 굴려, 매 순간 상단에 위치한 숫자를 출력하는 문제입니다.
Dice 클래스를 만들어 전역변수로 선언, 생성자로 시작 좌표를 받아 생성해주고 상하좌우전후 값은 모두 0으로 초기화 시켜줍니다.
이후에는 이동명령에 해당하는 방향에 맞게 주사위를 굴려 범위를 벗어났을 시 스킵, 범위 내의 유효한 값이라면 밟은 좌표의 값이 0인지 확인 후 각기 다른 조건을 수행해줍니다. 이동할때는 주사위의 현재 좌표값과 굴러간 방향으로 상하좌우전후 값만 서로 바꿔주면 이동이 끝났을 때 상단의 값을 출력할 수 있습니다.

📜 후기

구현문제는 설명이 너무 길어서 100% 이해를 못하고 넘어가는 것 같아요..
이번에도 주사위 면들의 초기값이 0이라는 조건을 못보고 1이 top에 있을 때의 주사위 값을 초기값으로 두어서 시간낭비했네요 ㅋㅋㅋㅋ
항상 문제 조건 읽는것에 집중!!

profile
머무르지 않기!

0개의 댓글