알고리즘/백준

[백준 14499] 주사위 굴리기 C++

겜도리도리 2021. 10. 13. 23:01
반응형

문제

백준 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

풀이

map에 관한 입력을 받고, 주어진 입력에 따라 주사위를 동서남북으로 움직이며 조건을 수행합니다.

소스 코드

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
#include <iostream>
using namespace std;
 
struct Dice
{
    // 북남동서 위아래
    int N = 0;
    int S = 0;
    int E = 0;
    int W = 0;
    int Top = 0;
    int Bottom = 0;
};
int map[20][20];
int N, M, y, x, K;
 
// 해당 방향으로 주사위 굴리기
void MoveE(Dice& dice) {
    int temp = dice.Top;
    dice.Top = dice.W;
    dice.W = dice.Bottom;
    dice.Bottom = dice.E;
    dice.E = temp;
}
 
void MoveW(Dice& dice) {
    int temp = dice.Top;
    dice.Top = dice.E;
    dice.E = dice.Bottom;
    dice.Bottom = dice.W;
    dice.W = temp;
}
 
void MoveN(Dice& dice) {
    int temp = dice.Top;
    dice.Top = dice.S;
    dice.S = dice.Bottom;
    dice.Bottom = dice.N;
    dice.N = temp;
}
 
void MoveS(Dice& dice) {
    int temp = dice.Top;
    dice.Top = dice.N;
    dice.N = dice.Bottom;
    dice.Bottom = dice.S;
    dice.S = temp;
}
 
void SetDice(Dice& dice) {
    // map이 0이면
    if (map[x][y] == 0) {
        map[x][y] = dice.Bottom;
    }
    // map이 0이 아니면
    else {
        dice.Bottom = map[x][y];
        map[x][y] = 0;
    }
}
 
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    cin >> N >> M >> x >> y >> K;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> map[i][j];
        }
    }
    Dice dice;
    int com;
    bool isPrint;
 
    for (int i = 0; i < K; i++) {
        cin >> com;
        isPrint = false;
        switch (com) {
        case 1:
            // 동
            if (y + 1 < M) {
                y++;
                MoveE(dice);
                SetDice(dice);
                isPrint = true;
            }
            break;
        case 2:
            // 서
            if (y - 1 >= 0) {
                y--;
                MoveW(dice);
                SetDice(dice);
                isPrint = true;
            }
            break;
        case 3:
            // 북
            if (x - 1 >= 0) {
                x--;
                MoveN(dice);
                SetDice(dice);
                isPrint = true;
            }
            break;
        case 4:
            // 남
            if (x + 1 < N) {
                x++;
                MoveS(dice);
                SetDice(dice);
                isPrint = true;
            }
            break;
        }
        if (isPrint) {
            cout << dice.Top << '\n';
        }
    }
    return 0;
}
cs
반응형