알고리즘 문제 풀이/BOJ

[backjoon]14499 주사위 굴리기 java

v 2022. 3. 28. 16:00

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

 

14499번: 주사위 굴리기

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지

www.acmicpc.net

이 문제에서 요구하는 것은 두 가지.

1. 주사위 굴리기

2. 방향에 맞게 이동하기

제일 막막한 주사위 굴리기.

그치만 한번 익히면 거의 공식 수준으로 쉽다.
1차원배열, 2차원배열 아무거나 써도 되고 구현하는 사람마다 다르지만 , 단순하게 얘기하면 주사위 전개도를 배열화 해서 그 안에서 값의 위치 이동만 시켜주면 된다.
북쪽으로 주사위가 굴려진다고 했을 때,
1의 자리에 있던 것이 5가 되고,
5의 자리로 6이 가고
6의 자리에 2가 가고
2의 자리에 1이 가는 것을 알 수 있다.
이를 이용해서 숫자만 바꿔주면 주사위는 쉽게 구현할 수 있다.

동서남북 각각의 방향으로 이동 후 전개도를 펼치면 노란색으로 칠한 값을 기준으로 값들이 변하는데 변하는 값들만 새로 넣어주면 된다.

배열의 칸 하나하나가 주사위의 위치인 것!

 

다음으로 주사위를 굴리고 나면, 지도에서 위치를 이동하고 지도가 0이면 주사위 값을 복사, 주사위가 0이라면 지도 값을 복사하면 된다.

풀이

 

 

 
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
import java.io.*;
import java.util.*;
 
class Main {
    static int[][] map;
    static int[] cur;
    static int N, M;
    static int[][] dirs = { { 10 }, { 01 }, { 0-1 }, { -10 } };
    static int[] dice;
 
    public static void main(String[] args) throws Exception {
        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 x = Integer.parseInt(st.nextToken());
        int y = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());
 
        // << 초기화 과정 >>
        map = new int[N][M];
        cur = new int[] { x, y };
        dice = new int[6];
        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());
        while (K-- > 0) {
            int moveDir = Integer.parseInt(st.nextToken()) % 4;
            move(moveDir);
        }
    }
 
    /**
     * 1. 해당 방향으로 이동하지 못하면 return
     * 
     * 2. 
     * - 이동가능하기에 주사위 값을 변경해준다. 
     * - 도착 위치가 0인지 아닌지에 따라 값 변경해주기. 
     * -새로운 위치 반영
     */
    public static void move(int dir) {
        int nextR = cur[0+ dirs[dir][0];
        int nextC = cur[1+ dirs[dir][1];
        
        if (nextR < 0 || nextC < 0 || nextR >= N || nextC >= M) {
            return;
        }
        
 
        rollDice(dir);
        
 
        if (map[nextR][nextC] == 0) {
            map[nextR][nextC] = dice[5];
        } else {
            dice[5= map[nextR][nextC];
            map[nextR][nextC] = 0;
        }
 
        cur = new int[] { nextR, nextC };
        System.out.println(dice[0]);
    }
 
    /**
     * 주사위 굴려져 변경된 정보를 해당 위치에 반영하기.
     */
    public static void rollDice(int dir) {
        int temp = 0;
        switch (dir) {
        case 1:
            temp = dice[0];
            dice[0= dice[3];
            dice[3= dice[5];
            dice[5= dice[2];
            dice[2= temp;
            break;
                
        case 2:
            temp = dice[0];
            dice[0= dice[2];
            dice[2= dice[5];
            dice[5= dice[3];
            dice[3= temp;
            break;
                
        case 3:
            temp = dice[0];
            dice[0= dice[4];
            dice[4= dice[5];
            dice[5= dice[1];
            dice[1= temp;
            break;
                
        default:
            temp = dice[0];
            dice[0= dice[1];
            dice[1= dice[5];
            dice[5= dice[4];
            dice[4= temp;
            break;
        }
    }
}
 
cs
반응형