Post

[JAVA]백준 14499번 주사위 굴리기

[JAVA]백준 14499번 주사위 굴리기

📌문제 링크


https://www.acmicpc.net/problem/14499

Image

📌문제 설명


주사위가 4방향으로 움직일때 값이 어떻게 변하는지만 구현하면 됩니다.

📌코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static Integer N, M, x, y, K; // 지도 크기와 시작 좌표, 명령어 개수
    static int[] dice = {0,0,0,0,0,0}; // 주사위 상태
    static int[][] mp = new int[21][21]; // 지도
    static int[] order = new int[1001]; // 명령어 배열

    public static void main(String[] args) throws Exception {
        input(); // 입력 받기
        solve(); // 문제 해결
    }

    // 입력을 받는 메서드
    public static void input() throws Exception {
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken()); // 지도 세로 크기
        M = Integer.parseInt(st.nextToken()); // 지도 가로 크기
        x = Integer.parseInt(st.nextToken()); // 시작 x 좌표
        y = Integer.parseInt(st.nextToken()); // 시작 y 좌표
        K = Integer.parseInt(st.nextToken()); // 명령어 개수

        // 지도 정보 입력 받기
        for(int i = 0; i < N; i++){
            st = new StringTokenizer(br.readLine());
            for(int j = 0; j < M; j++){
                mp[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        // 명령어 입력 받기
        st = new StringTokenizer(br.readLine());
        for(int i = 0; i < K; i++){
            order[i] = Integer.parseInt(st.nextToken());
        }
        br.close(); // 입력 스트림 닫기
    }

    // 좌표가 유효한 범위인지 확인하는 메서드
    public static Boolean isRange(int a, int b) {
        return a >= 0 && a < N && b >= 0 && b < M;
    }

    // 동쪽으로 이동하는 메서드
    public static void move1() throws Exception {
        if(!isRange(x, y + 1)) {
            return;
        }
        y += 1;
        int temp = dice[5];
        dice[5] = dice[3];
        dice[3] = dice[2];
        dice[2] = dice[1];
        dice[1] = temp;
        if(mp[x][y] == 0) {
            mp[x][y] = dice[5];
        } else {
            dice[5] = mp[x][y];
            mp[x][y] = 0;
        }
        bw.write(dice[2] + "\n");
    }

    // 서쪽으로 이동하는 메서드
    public static void move2() throws Exception {
        if(!isRange(x, y - 1)) {
            return;
        }
        y -= 1;
        int temp = dice[5];
        dice[5] = dice[1];
        dice[1] = dice[2];
        dice[2] = dice[3];
        dice[3] = temp;
        if(mp[x][y] == 0) {
            mp[x][y] = dice[5];
        } else {
            dice[5] = mp[x][y];
            mp[x][y] = 0;
        }
        bw.write(dice[2] + "\n");
    }

    // 북쪽으로 이동하는 메서드
    public static void move3() throws Exception {
        if(!isRange(x - 1, y)) {
            return;
        }
        x -= 1;
        int temp = dice[5];
        dice[5] = dice[4];
        dice[4] = dice[2];
        dice[2] = dice[0];
        dice[0] = temp;
        if(mp[x][y] == 0) {
            mp[x][y] = dice[5];
        } else {
            dice[5] = mp[x][y];
            mp[x][y] = 0;
        }
        bw.write(dice[2] + "\n");
    }

    // 남쪽으로 이동하는 메서드
    public static void move4() throws Exception {
        if(!isRange(x + 1, y)) {
            return;
        }
        x += 1;
        int temp = dice[0];
        dice[0] = dice[2];
        dice[2] = dice[4];
        dice[4] = dice[5];
        dice[5] = temp;
        if(mp[x][y] == 0) {
            mp[x][y] = dice[5];
        } else {
            dice[5] = mp[x][y];
            mp[x][y] = 0;
        }
        bw.write(dice[2] + "\n");
    }

    // 명령어를 처리하는 메서드
    public static void solve() throws Exception {
        for (int i = 0; i < K; i++) {
            int dir = order[i];
            switch (dir) {
                case 1 -> move1();
                case 2 -> move2();
                case 3 -> move3();
                case 4 -> move4();
            }
        }
        bw.flush(); // 출력 버퍼 비우기
        bw.close(); // 출력 스트림 닫기
    }
}
This post is licensed under CC BY 4.0 by the author.