본문 바로가기
Programming/Algorithm

백준 14499 주사위 굴리기

by OKOK 2018. 4. 14.

설계 과정

1. 문제를 꼼꼼히 읽습니다.

2. 설계를 정확하게 합니다. (예제 3개 돌리기)

3. 경우의 수를 나열합니다.

4. 초기화 변수를 확인합니다.

5. 가지치기를 합니다.

6. 예제와 동일하게 변수를 선언하고 사용합니다.


풀이 과정

1. 주사위를 굴린다음에,

2. 주사위의 바닥면과 맵을 비교해줍니다.

3. 주사위의 윗면을 출력합니다.


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
/*
14499 주사위 굴리기
18:39 시작
19:34
55분컷
설계 과정
1. 문제를 정확하게 읽습니다.
2. 설계를 완벽하게 합니다. (예제 3개 돌리기)
3. 경우의 수를 나열합니다.
4. 초기화 변수를 확인합니다.
5. 가지치기를 합니다.
6. 예제와 동일하게 변수를 선언하고 사용합니다.
풀이 과정
1. 입력을 받습니다.
2. 굴러가는 것을 명확히 합니다.
3. 굴릴 때, 조건에 따라서 맵이 변경됩니다.
- 주사위 스왑하는 부분을 주의합니다.
*/
 
#include <iostream>
#include <algorithm>
using namespace std;
 
#define SIZE 25
int N, M, startX, startY, K;
int map[SIZE][SIZE];
int command[1005];
int dice[7];
int dx[] = { 0,0,0,-1,1 };
int dy[] = { 0,1,-1,0,0 };
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 init() {
 
}
 
void recovery_dice(int dir) {
    if (dir == 1) {
        int tmp = dice[1];
        dice[1= dice[5];
        dice[5= dice[3];
        dice[3= dice[6];
        dice[6= tmp;
    }
    else if (dir == 2) {
        int tmp = dice[1];
        dice[1= dice[6];
        dice[6= dice[3];
        dice[3= dice[5];
        dice[5= tmp;
    }
    else if (dir == 3) {
        int tmp = dice[1];
        dice[1= dice[2];
        dice[2= dice[3];
        dice[3= dice[4];
        dice[4= tmp;
    }
 
    else if (dir == 4) {
        int tmp = dice[1];
        dice[1= dice[4];
        dice[4= dice[3];
        dice[3= dice[2];
        dice[2= tmp;
    }
}
 
void turn_dice(int dir) {
 
    nx = x + dx[dir];
    ny = y + dy[dir];
    if (nx < 0 || ny < 0 || nx >= N || ny >= M) {
        return;
    }
 
    recovery_dice(dir);
 
    if (map[nx][ny] == 0) {
        map[nx][ny] = dice[3];
    }
    else {
        dice[3= map[nx][ny];
        map[nx][ny] = 0;
    }
    x = nx;
    y = ny;
    cout << dice[1<< endl;    
}
 
void solve() {
 
    x = startX;
    y = startY;
    for (int i = 0; i < K; i++) {
        turn_dice(command[i]);
    }
}
 
int main() {
    problemIn();
    solve();
    return 0;
}
 
 
cs