알고리즘

[BOJ #5373] 큐빙(반례 포함), C++

kdjames0930 2025. 1. 4. 15:27

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

 

[문제 요약]

 

문제에서 주어진 주사위 (출처: 백준)

 

3X3 크기의 큐브가 있고, 각 면은 색칠되어 있다. 

(앞: 빨간색, 뒤: 주황색, 위: 흰색, 아래: 노란색, 왼: 초록색, 오른: 파란색)

이 큐브를 입력에서 주어진대로 회전을 하고, 회전이 끝났을 때 윗 면의 색상을 출력하면 된다!

 

[문제 풀이]

 

비록 플레티넘으로 랭크되어있는 문제지만, 어려운 알고리즘이 사용되지는 않는다.

단순히 모든 면을 시계방향/반시계방향으로 돌리는 경우를 각각 구현해주면 된다. 

 

char front[3][3];
char up[3][3];
char down[3][3];
char lft[3][3];
char rght[3][3];
char back[3][3];
char temp[3];

 

우선 각 면의 색상 정보를 3x3 배열로 만들었다.

 

void turn_up(char pm){
    if(pm=='+'){
        for(int i=0; i<3; i++) temp[i]=front[0][i];
        for(int i=0; i<3; i++) front[0][i]=rght[0][i];
        for(int i=0; i<3; i++) rght[0][i]=back[0][i];
        for(int i=0; i<3; i++) back[0][i]=lft[0][i];
        for(int i=0; i<3; i++) lft[0][i]=temp[i];
        temp[0]=up[0][0];
        up[0][0]=up[2][0];
        up[2][0]=up[2][2];
        up[2][2]=up[0][2];
        up[0][2]=temp[0];
        temp[0]=up[0][1];
        up[0][1]=up[1][0];
        up[1][0]=up[2][1];
        up[2][1]=up[1][2];
        up[1][2]=temp[0];
    }
    else{
        for(int i=0; i<3; i++) temp[i]=front[0][i];
        for(int i=0; i<3; i++) front[0][i]=lft[0][i];
        for(int i=0; i<3; i++) lft[0][i]=back[0][i];
        for(int i=0; i<3; i++) back[0][i]=rght[0][i];
        for(int i=0; i<3; i++) rght[0][i]=temp[i];
        temp[0]=up[0][0];
        up[0][0]=up[0][2];
        up[0][2]=up[2][2];
        up[2][2]=up[2][0];
        up[2][0]=temp[0];
        temp[0]=up[0][1];
        up[0][1]=up[1][2];
        up[1][2]=up[2][1];
        up[2][1]=up[1][0];
        up[1][0]=temp[0];
    }
}

 

윗 면을 시계방향으로 돌릴 때와 반시계방향으로 돌릴 때를 구현한 것이다. 이 작업을 나머지 5개 면에 대해서도 구현해주면 된다..! (인덱스 오류를 저지르기 쉬운 것 같다)

 

[반례]

 

회전을 한 번 혹은 두 번씩 했을 때 출력이 올바르게 나오는지 확인하고, 코드의 어떤 부분에서 오류가 발생했는지 확인하는 것이 좋아보인다. 위에서부터 하나씩 올바르게 출력이 되는지 시도해보자!

1
1
F+
www
www
ggg

1
1
F-
www
www
bbb

1
1
B+
bbb
www
www

1
1
B-
ggg
www
www

1
1
L+
oww
oww
oww

1
1
L-
rww
rww
rww

1
1
R+
wwr
wwr
wwr

1
1
R-
wwo
wwo
wwo

1
2
R+ U+
www
www
rrr

1
2
R+ U-
rrr
www
www

1
2
D+ R+
wwr
wwr
wwg

1
2
D- L+
gww
oww
oww

 

[전체 코드]

 

#include <iostream>
#include <string>

using namespace std;

char front[3][3];
char up[3][3];
char down[3][3];
char lft[3][3];
char rght[3][3];
char back[3][3];
char temp[3];
string stri;

void turn_up(char pm){
    if(pm=='+'){
        for(int i=0; i<3; i++) temp[i]=front[0][i];
        for(int i=0; i<3; i++) front[0][i]=rght[0][i];
        for(int i=0; i<3; i++) rght[0][i]=back[0][i];
        for(int i=0; i<3; i++) back[0][i]=lft[0][i];
        for(int i=0; i<3; i++) lft[0][i]=temp[i];
        temp[0]=up[0][0];
        up[0][0]=up[2][0];
        up[2][0]=up[2][2];
        up[2][2]=up[0][2];
        up[0][2]=temp[0];
        temp[0]=up[0][1];
        up[0][1]=up[1][0];
        up[1][0]=up[2][1];
        up[2][1]=up[1][2];
        up[1][2]=temp[0];
    }
    else{
        for(int i=0; i<3; i++) temp[i]=front[0][i];
        for(int i=0; i<3; i++) front[0][i]=lft[0][i];
        for(int i=0; i<3; i++) lft[0][i]=back[0][i];
        for(int i=0; i<3; i++) back[0][i]=rght[0][i];
        for(int i=0; i<3; i++) rght[0][i]=temp[i];
        temp[0]=up[0][0];
        up[0][0]=up[0][2];
        up[0][2]=up[2][2];
        up[2][2]=up[2][0];
        up[2][0]=temp[0];
        temp[0]=up[0][1];
        up[0][1]=up[1][2];
        up[1][2]=up[2][1];
        up[2][1]=up[1][0];
        up[1][0]=temp[0];
    }
}
void turn_down(char pm){
    if(pm=='+'){
        for(int i=0; i<3; i++) temp[i]=front[2][i];
        for(int i=0; i<3; i++) front[2][i]=lft[2][i];
        for(int i=0; i<3; i++) lft[2][i]=back[2][i];
        for(int i=0; i<3; i++) back[2][i]=rght[2][i];
        for(int i=0; i<3; i++) rght[2][i]=temp[i];
        temp[0]=down[0][0];
        down[0][0]=down[2][0];
        down[2][0]=down[2][2];
        down[2][2]=down[0][2];
        down[0][2]=temp[0];
        temp[0]=down[0][1];
        down[0][1]=down[1][0];
        down[1][0]=down[2][1];
        down[2][1]=down[1][2];
        down[1][2]=temp[0];
    }
    else{
        for(int i=0; i<3; i++) temp[i]=front[2][i];
        for(int i=0; i<3; i++) front[2][i]=rght[2][i];
        for(int i=0; i<3; i++) rght[2][i]=back[2][i];
        for(int i=0; i<3; i++) back[2][i]=lft[2][i];
        for(int i=0; i<3; i++) lft[2][i]=temp[i];
        temp[0]=down[0][0];
        down[0][0]=down[0][2];
        down[0][2]=down[2][2];
        down[2][2]=down[2][0];
        down[2][0]=temp[0];
        temp[0]=down[0][1];
        down[0][1]=down[1][2];
        down[1][2]=down[2][1];
        down[2][1]=down[1][0];
        down[1][0]=temp[0];;
    }
}
void turn_front(char pm){
    if(pm=='+'){
        for(int i=0; i<3; i++) temp[i]=up[2][i];
        for(int i=0; i<3; i++) up[2][i]=lft[2-i][2];
        for(int i=0; i<3; i++) lft[i][2]=down[0][i];
        for(int i=0; i<3; i++) down[0][i]=rght[2-i][0];
        for(int i=0; i<3; i++) rght[i][0]=temp[i];
        temp[0]=front[0][0];
        front[0][0]=front[2][0];
        front[2][0]=front[2][2];
        front[2][2]=front[0][2];
        front[0][2]=temp[0];
        temp[0]=front[0][1];
        front[0][1]=front[1][0];
        front[1][0]=front[2][1];
        front[2][1]=front[1][2];
        front[1][2]=temp[0];
    }
    else{
        for(int i=0; i<3; i++) temp[i]=up[2][i];
        for(int i=0; i<3; i++) up[2][i]=rght[i][0];
        for(int i=0; i<3; i++) rght[i][0]=down[0][2-i];
        for(int i=0; i<3; i++) down[0][i]=lft[i][2];
        for(int i=0; i<3; i++) lft[i][2]=temp[2-i];
        temp[0]=front[0][0];
        front[0][0]=front[0][2];
        front[0][2]=front[2][2];
        front[2][2]=front[2][0];
        front[2][0]=temp[0];
        temp[0]=front[0][1];
        front[0][1]=front[1][2];
        front[1][2]=front[2][1];
        front[2][1]=front[1][0];
        front[1][0]=temp[0];;
    }
}
void turn_back(char pm){
    if(pm=='+'){
        for(int i=0; i<3; i++) temp[i]=up[0][i];
        for(int i=0; i<3; i++) up[0][i]=rght[i][2];
        for(int i=0; i<3; i++) rght[i][2]=down[2][2-i];
        for(int i=0; i<3; i++) down[2][i]=lft[i][0];
        for(int i=0; i<3; i++) lft[i][0]=temp[2-i];
        temp[0]=back[0][0];
        back[0][0]=back[2][0];
        back[2][0]=back[2][2];
        back[2][2]=back[0][2];
        back[0][2]=temp[0];
        temp[0]=back[0][1];
        back[0][1]=back[1][0];
        back[1][0]=back[2][1];
        back[2][1]=back[1][2];
        back[1][2]=temp[0];
    }
    else{
        for(int i=0; i<3; i++) temp[i]=up[0][i];
        for(int i=0; i<3; i++) up[0][i]=lft[2-i][0];
        for(int i=0; i<3; i++) lft[i][0]=down[2][i];
        for(int i=0; i<3; i++) down[2][i]=rght[2-i][2];
        for(int i=0; i<3; i++) rght[i][2]=temp[i];
        temp[0]=back[0][0];
        back[0][0]=back[0][2];
        back[0][2]=back[2][2];
        back[2][2]=back[2][0];
        back[2][0]=temp[0];
        temp[0]=back[0][1];
        back[0][1]=back[1][2];
        back[1][2]=back[2][1];
        back[2][1]=back[1][0];
        back[1][0]=temp[0];
    }
}
void turn_left(char pm){
    if(pm=='+'){
        for(int i=0; i<3; i++) temp[i]=up[i][0];
        for(int i=0; i<3; i++) up[i][0]=back[2-i][2];
        for(int i=0; i<3; i++) back[i][2]=down[2-i][0];
        for(int i=0; i<3; i++) down[i][0]=front[i][0];
        for(int i=0; i<3; i++) front[i][0]=temp[i];
        temp[0]=lft[0][0];
        lft[0][0]=lft[2][0];
        lft[2][0]=lft[2][2];
        lft[2][2]=lft[0][2];
        lft[0][2]=temp[0];
        temp[0]=lft[0][1];
        lft[0][1]=lft[1][0];
        lft[1][0]=lft[2][1];
        lft[2][1]=lft[1][2];
        lft[1][2]=temp[0];
    }
    else{
        for(int i=0; i<3; i++) temp[i]=up[i][0];
        for(int i=0; i<3; i++) up[i][0]=front[i][0];
        for(int i=0; i<3; i++) front[i][0]=down[i][0];
        for(int i=0; i<3; i++) down[i][0]=back[2-i][2];
        for(int i=0; i<3; i++) back[i][2]=temp[2-i];
        temp[0]=lft[0][0];
        lft[0][0]=lft[0][2];
        lft[0][2]=lft[2][2];
        lft[2][2]=lft[2][0];
        lft[2][0]=temp[0];
        temp[0]=lft[0][1];
        lft[0][1]=lft[1][2];
        lft[1][2]=lft[2][1];
        lft[2][1]=lft[1][0];
        lft[1][0]=temp[0];
    }
}
void turn_right(char pm){
    if(pm=='+'){
        for(int i=0; i<3; i++) temp[i]=up[i][2];
        for(int i=0; i<3; i++) up[i][2]=front[i][2];
        for(int i=0; i<3; i++) front[i][2]=down[i][2];
        for(int i=0; i<3; i++) down[i][2]=back[2-i][0];
        for(int i=0; i<3; i++) back[i][0]=temp[2-i];
        temp[0]=rght[0][0];
        rght[0][0]=rght[2][0];
        rght[2][0]=rght[2][2];
        rght[2][2]=rght[0][2];
        rght[0][2]=temp[0];
        temp[0]=rght[0][1];
        rght[0][1]=rght[1][0];
        rght[1][0]=rght[2][1];
        rght[2][1]=rght[1][2];
        rght[1][2]=temp[0];
    }
    else{
        for(int i=0; i<3; i++) temp[i]=up[i][2];
        for(int i=0; i<3; i++) up[i][2]=back[2-i][0];
        for(int i=0; i<3; i++) back[i][0]=down[2-i][2];
        for(int i=0; i<3; i++) down[i][2]=front[i][2];
        for(int i=0; i<3; i++) front[i][2]=temp[i];
        temp[0]=rght[0][0];
        rght[0][0]=rght[0][2];
        rght[0][2]=rght[2][2];
        rght[2][2]=rght[2][0];
        rght[2][0]=temp[0];
        temp[0]=rght[0][1];
        rght[0][1]=rght[1][2];
        rght[1][2]=rght[2][1];
        rght[2][1]=rght[1][0];
        rght[1][0]=temp[0];
    }
}

int main(){
    int t, n;
    char order[2];
    cin>>t;
    for(int i=0; i<t; i++){
        for(int q=0; q<3; q++){
            for(int w=0; w<3; w++){
                front[q][w]='r';
                up[q][w]='w';
                down[q][w]='y';
                back[q][w]='o';
                lft[q][w]='g';
                rght[q][w]='b';
            }
        }
        cin>>n;
        for (int j = 0; j < n; j++) {
            cin >> stri;
            char face = stri[0];
            char direction = stri[1];
            if (face == 'U') {
                turn_up(direction);
            } else if (face == 'D') {
                turn_down(direction);
            } else if (face == 'F') {
                turn_front(direction);
            } else if (face == 'B') {
                turn_back(direction);
            } else if (face == 'L') {
                turn_left(direction);
            } else if (face == 'R') {
                turn_right(direction);
            }
        }
        for(int k=0; k<3; k++){
            for(int p=0; p<3; p++){
                cout<<up[k][p];
            }
            cout<<endl;
        }
    }
}

 

 

 

반응형

'알고리즘' 카테고리의 다른 글

[BOJ #1655] 가운데를 말해요, C++  (8) 2025.01.08
[BOJ #16236] 아기상어, C++  (11) 2025.01.03