백준 주사위 굴리기 14499

피나코·2023년 1월 21일
0

알고리즘

목록 보기
35/46
post-thumbnail

백준 주사위 굴리기 14499 바로가기

문제 설명

크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 이 지도의 위에 주사위가 하나 놓여져 있으며, 주사위의 전개도는 아래와 같다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다.

  2
4 1 3
  5
  6

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

지도의 각 칸에는 정수가 하나씩 쓰여져 있다. 주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사된다. 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0이 된다.

주사위를 놓은 곳의 좌표와 이동시키는 명령이 주어졌을 때, 주사위가 이동했을 때 마다 상단에 쓰여 있는 값을 구하는 프로그램을 작성하시오.

주사위는 지도의 바깥으로 이동시킬 수 없다. 만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 된다.

접근 방식

단순한 구현/시뮬레이션 문제였다.
주사위 객체를 만들어주어서 해결했다.

코드

package Algorithm_Breathing.BOJ;

import java.io.*;

public class Main_14499 {
    static int N, M;
    static int[] dx = {-22, 0, 0, -1, 1};
    static int[] dy = {-22, 1, -1, 0, 0};

    static int[][] map;

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        String[] a = br.readLine().split(" ");
        N = Integer.parseInt(a[0]);
        M = Integer.parseInt(a[1]);
        int x = Integer.parseInt(a[2]);
        int y = Integer.parseInt(a[3]);
        int K = Integer.parseInt(a[4]);

        map = new int[N][M];

        for (int i = 0; i < N; i++) {
            String[] line = br.readLine().split(" ");
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(line[j]);
            }
        }

        String[] inst = br.readLine().split(" ");

        Dice dice = new Dice(0, 0, 0, 0, 0, 0, x, y);

        for (String i : inst) {
            int d = Integer.parseInt(i);

            int res = dice.move(d);

            if (res == -1) continue;

            sb.append(res).append("\n");

        }

        bw.write(sb.toString());
        bw.flush();
        bw.close();
        br.close();

    }

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

        public Dice(int top, int bottom, int left, int right, int front, int back, int x, int y) {
            this.top = top;
            this.bottom = bottom;
            this.left = left;
            this.right = right;
            this.front = front;
            this.back = back;
            this.x = x;
            this.y = y;
        }

        public int move(int d) {
            if (x + dx[d] < 0 || x + dx[d] == N || y + dy[d] < 0 || y + dy[d] == M)
                return -1;

            this.x += dx[d];
            this.y += dy[d];

            if (d == 1) moveEast();
            else if (d == 2) moveWest();
            else if (d == 3) moveNorth();
            else moveSouth();

            return top;

        }

        private void moveEast() {
            int temp = right;

            right = top;
            top = left;
            left = bottom;
            changeBottom(temp);
        }

        private void moveWest() {
            int temp = left;

            left = top;
            top = right;
            right = bottom;
            changeBottom(temp);
        }

        private void moveNorth() {
            int temp = back;

            back = top;
            top = front;
            front = bottom;
            changeBottom(temp);
        }

        private void moveSouth() {
            int temp = front;

            front = top;
            top = back;
            back = bottom;
            changeBottom(temp);
        }

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

}

Disscussion

오랜만에 푼 구현문제였다. 재밌었다!

profile
_thisispinako_

0개의 댓글