본문 바로가기
백준

백준 5373 큐빙(SW 역량 테스트 기출 문제)

by 콩순이냉장고 2020. 9. 21.

문제 URL : www.acmicpc.net/problem/5373

 

5373번: 큐빙

각 테스트 케이스에 대해서 큐브를 모두 돌린 후의 윗 면의 색상을 출력한다. 첫 번째 줄에는 뒷 면과 접하는 칸의 색을 출력하고, 두 번째, 세 번째 줄은 순서대로 출력하면 된다. 흰색은 w, 노란

www.acmicpc.net

문제 접근법: 큐브 모양을 회전했을때 윗면이 어떤 색을 갖고있는지 출력하는 문제이다.

이것은 글로만 설명하긴 너무어렵기에 그림이 필요합니다.

큐브의 전개도를 그렸을때

큐빙전개도

이런모양의 전개도가 나옵니다. 사람마다 어떤게 잡았는지는 약간 차이가 있을겁니다.

큐브의 색깔을 초기에 어떤것을 가르키는지 이해하기 쉽게 그렸습니다.

 맨왼쪽 인덱스가 0이면 흰색이고 1이면 빨강 2이면 노랑으로 해서 5번까지 5번은 파랑을 가리킵니다. 

원하는 면을 회전할때 어떻게 바뀌는지 보겠습니다.

처음 원하는 2차원 평면을 회전할경우

시계방향으로 90도 회전

하지만 이것은 3차원 큐브이므로 원하는 면주위에 12개의 다른면들이 붙어있습니다.

만약 앞면을 회전할경우 

앞쪽면 90도회전

앞쪽면의 전개도를 땐것처럼 생각할수있으니 그럼 오른쪽면은 어떻게 붙어있는지 보여드리겠습니다.

오른쪽면

이문제의 첫번째 접근은 전개도를 그리고 그문제를 어떻게 표현하는것이 

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//By 콩순이냉장고
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
#include<algorithm>
#include <cstring>
using namespace std;
int cube[6][3][3];
unordered_map<charint> toidx;
void print(int num)//색깔별로 맞는것을 출력
{
    if (num == 0)
        cout << "w";
    else if (num == 1)
        cout << "r";
    else if (num == 2)
        cout << "y";
    else if (num == 3)
        cout << "o";
    else if (num == 4)
        cout << "g";
    else
        cout << "b";
}
 
void rotate(int idx)//시계방향으로 회전
{
    int temp[6][3][3];
    memcpy(temp, cube,sizeof(cube));
    int i2 = 0;
    for (int j = 2; j >= 0; j--)//회전할 면 회전
    {
        int j2 = 0;
        for (int i = 0; i < 3; i++)
        {
            cube[idx][i][j] = temp[idx][i2][j2++];
        }
        i2++;
    }
 
    //면주위에있는 12개 회전 이게참 노가다입니다.
    switch (idx){
    case 0:
        cube[3][2][0= temp[4][0][2];
        cube[3][2][1= temp[4][0][1];
        cube[3][2][2= temp[4][0][0];
        
        cube[5][0][2= temp[3][2][0];
        cube[5][0][1= temp[3][2][1];
        cube[5][0][0= temp[3][2][2];
 
        cube[1][0][2= temp[5][0][2];
        cube[1][0][1= temp[5][0][1];
        cube[1][0][0= temp[5][0][0];
 
        cube[4][0][2= temp[1][0][2];
        cube[4][0][1= temp[1][0][1];
        cube[4][0][0= temp[1][0][0];
        break;
    case 1:
        cube[0][2][0= temp[4][2][2];
        cube[0][2][1= temp[4][1][2];
        cube[0][2][2= temp[4][0][2];
 
        cube[5][0][0= temp[0][2][0];
        cube[5][1][0= temp[0][2][1];
        cube[5][2][0= temp[0][2][2];
 
        cube[2][0][2= temp[5][0][0];
        cube[2][0][1= temp[5][1][0];
        cube[2][0][0= temp[5][2][0];
 
        cube[4][2][2= temp[2][0][2];
        cube[4][1][2= temp[2][0][1];
        cube[4][0][2= temp[2][0][0];
        break;
    case 2:
        cube[1][2][0= temp[4][2][0];
        cube[1][2][1= temp[4][2][1];
        cube[1][2][2= temp[4][2][2];
 
        cube[5][2][0= temp[1][2][0];
        cube[5][2][1= temp[1][2][1];
        cube[5][2][2= temp[1][2][2];
 
        cube[3][0][2= temp[5][2][0];
        cube[3][0][1= temp[5][2][1];
        cube[3][0][0= temp[5][2][2];
        
        cube[4][2][0= temp[3][0][2];
        cube[4][2][1= temp[3][0][1];
        cube[4][2][2= temp[3][0][0];
        break;
    case 3:
        cube[2][2][0= temp[4][0][0];
        cube[2][2][1= temp[4][1][0];
        cube[2][2][2= temp[4][2][0];
 
        cube[5][2][2= temp[2][2][0];
        cube[5][1][2= temp[2][2][1];
        cube[5][0][2= temp[2][2][2];
 
        cube[0][0][2= temp[5][2][2];
        cube[0][0][1= temp[5][1][2];
        cube[0][0][0= temp[5][0][2];
 
        cube[4][0][0= temp[0][0][2];
        cube[4][1][0= temp[0][0][1];
        cube[4][2][0= temp[0][0][0];
        break;
    case 4:
        cube[0][0][0= temp[3][0][0];
        cube[0][1][0= temp[3][1][0];
        cube[0][2][0= temp[3][2][0];
 
        cube[1][0][0= temp[0][0][0];
        cube[1][1][0= temp[0][1][0];
        cube[1][2][0= temp[0][2][0];
 
        cube[2][0][0= temp[1][0][0];
        cube[2][1][0= temp[1][1][0];
        cube[2][2][0= temp[1][2][0];
 
        cube[3][0][0= temp[2][0][0];
        cube[3][1][0= temp[2][1][0];
        cube[3][2][0= temp[2][2][0];
        break;
    case 5:
        cube[0][2][2= temp[1][2][2];
        cube[0][1][2= temp[1][1][2];
        cube[0][0][2= temp[1][0][2];
 
        cube[3][2][2= temp[0][2][2];
        cube[3][1][2= temp[0][1][2];
        cube[3][0][2= temp[0][0][2];
 
        cube[2][2][2= temp[3][2][2];
        cube[2][1][2= temp[3][1][2];
        cube[2][0][2= temp[3][0][2];
 
        cube[1][2][2= temp[2][2][2];
        cube[1][1][2= temp[2][1][2];
        cube[1][0][2= temp[2][0][2];
        break;
    }
}
void init()
{
    toidx['U'= 0;
    toidx['F'= 1;
    toidx['D'= 2;
    toidx['B'= 3;
    toidx['L'= 4;
    toidx['R'= 5;
 
    for (int i = 0; i < 6; i++)//이게 색깔이라고 생각하세요
    {
        for (int j = 0; j < 3; j++)
        {
            for (int k = 0; k < 3; k++)
                cube[i][j][k] = i;
        }
    }
 
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    int test;
    cin >> test;
    while (test--){
        init();
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            string s;
            cin >> s;
            if (s[1== '+')
                rotate(toidx[s[0]]);
            else//반시계방향은 시계방향으로 3번회전 한것과 결과가 같음
            {
                rotate(toidx[s[0]]);
                rotate(toidx[s[0]]);
                rotate(toidx[s[0]]);
            }
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
                print(cube[0][i][j]);
            cout << "\n";
        }
    }
}
 
 
cs

 

궁금한점 혹은 모르는점 혹은 논리가 틀린점이 있다면 언제든지 댓글을 이용해주시길 바랍니다.

 

'백준' 카테고리의 다른 글

백준 16562 친구비  (0) 2020.10.02
백준 10216 Count Circle Groups  (0) 2020.10.02
백준 4195 친구 네트워크  (0) 2020.08.30
백준 4386 별자리 만들기  (0) 2020.08.30
백준 1922 네트워크 연결  (0) 2020.08.30