본문 바로가기
Programming/Algorithm

백준 14499 주사위 굴리기

by OKOK 2018. 4. 6.

1. 27분컷

2. 내용에 써있는대로 바로바로 코딩합니다.

3. 왜곡하지 말고 바로 씁니다.

4. 오께이.

5. 


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
/*
1732 주사위를 굴려봅시다.
*/
 
#include <iostream>
using namespace std;
 
#define SIZE 25
 
int map[SIZE][SIZE];
int N, M, startX, startY, K;
int command[1000];
int dice[7];
int dx[] = { 0,0,0,-1,1 };
int dy[] = { 0,1,-1,0,0 }; // 1234 동 서 북 남
int x, y, nx, ny;
 
 
void problemIn() {
    cin >> N >> M >> startX >> startY >> K;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> map[i][j];
        }
    }
    for (int i = 0; i < K; i++) {
        cin >> command[i];
    }
}
 
void dice_turn(int a) {
 
    if (a == 1) {
        int tmp = dice[4];
        dice[4= dice[1];
        dice[1= dice[3];
        dice[3= dice[6];
        dice[6= tmp;
    }
    else if (a == 2) {
        int tmp = dice[4];
        dice[4= dice[6];
        dice[6= dice[3];
        dice[3= dice[1];
        dice[1= tmp;
    }
    else if (a == 3) {
        int tmp = dice[1];
        dice[1= dice[5];
        dice[5= dice[6];
        dice[6= dice[2];
        dice[2= tmp;
    }
 
    else if (a == 4) {
        int tmp = dice[1];
        dice[1= dice[2];
        dice[2= dice[6];
        dice[6= dice[5];
        dice[5= tmp;
    }
}
 
 
void solve() {
    x = startX;
    y = startY;
 
    for (int i = 0; i < K; i++) {
        nx = x + dx[command[i]];
        ny = y + dy[command[i]];
        if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue// 다음 이동이 바깥이면, 무시합니다.
        dice_turn(command[i]);
        if (map[nx][ny] == 0) { // 바닥면이 0 인 경우에는,
            map[nx][ny] = dice[6]; // 바닥에 복사.
        }
        else { // 바닥면이 0이 아닌 경우에는,
            dice[6= map[nx][ny]; 
            map[nx][ny] = 0;
        }
        x = nx;
        y = ny;
        cout << dice[1<< endl;
    }
}
 
int main() {
    problemIn();
    solve();
    return 0;
}
cs