[백준] 5373번: 큐빙

2022. 10. 13. 18:39TIL💡/Algorithms

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

 

5373번: 큐빙

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

www.acmicpc.net

무려 이틀에 걸쳐 푼 문제이다.

쌩구현을 해야했는데, 나름 쉽게 풀려면 입체적인 큐브를 상상하면서 규칙을 생성해야 한다.

큐브를 한 번 돌리면 배열에는 크게 2가지 변화가 생긴다.

  • 돌아가는 해당 면의 위치 변경
  • 인접한 면들의 회전

그래서 이를 나누어서 큐브에 반영해준다.

큐브는 정육면체이므로 6개의 배열에 3 by 3 2차원 배열을 넣어 각 방향마다 값들을 옮겨준다.

만약 같은 면을 돌리되 방향만 다른 경우라면 움직이는 값들도 방향만 다르기 때문에 이에 유의해서 값들을 넣어간다면 시간을 조금이라도 단축할 수 있다. 하지만 문제가 복잡해 디버깅도 쉽지 않았다ㅠ

 

그래도 만약 실전에서 이런 문제를 발견하면 가차없이 다른 문제를 풀기 시작해야 한다..!!

 

#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
int t, n;

void rotate(int layer_idx, vector<vector<vector<char>>> &cube, int dir) {
    vector<vector<char>> tmp(3, vector<char>(3));

    if (dir == 0) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                tmp[j][2 - i] = cube[layer_idx][i][j];
            }
        }
    }
    else {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                tmp[2 - j][i] = cube[layer_idx][i][j];
            }
        }
    }

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cube[layer_idx][i][j] = tmp[i][j];
        }
    }
}

void rotate_dimensions(int layer_idx, vector<vector<vector<char>>>& cube, int dir) {
    char tmp1, tmp2, tmp3;
    if (layer_idx == 0) {
        if (dir == 0) {
            tmp1 = cube[4][0][2];
            tmp2 = cube[4][1][2];
            tmp3 = cube[4][2][2];

            cube[4][0][2] = cube[1][0][0];
            cube[4][1][2] = cube[1][0][1];
            cube[4][2][2] = cube[1][0][2];

            cube[1][0][0] = cube[5][2][0];
            cube[1][0][1] = cube[5][1][0];
            cube[1][0][2] = cube[5][0][0];

            cube[5][2][0] = cube[3][2][2];
            cube[5][1][0] = cube[3][2][1];
            cube[5][0][0] = cube[3][2][0];

            cube[3][2][2] = tmp1;
            cube[3][2][1] = tmp2;
            cube[3][2][0] = tmp3;
        }
        else {
            tmp1 = cube[4][0][2];
            tmp2 = cube[4][1][2];
            tmp3 = cube[4][2][2];

            cube[4][0][2] = cube[3][2][2];
            cube[4][1][2] = cube[3][2][1];
            cube[4][2][2] = cube[3][2][0];

            cube[3][2][2] = cube[5][2][0];
            cube[3][2][1] = cube[5][1][0];
            cube[3][2][0] = cube[5][0][0];

            cube[5][2][0] = cube[1][0][0];
            cube[5][1][0] = cube[1][0][1];
            cube[5][0][0] = cube[1][0][2];

            cube[1][0][0] = tmp1;
            cube[1][0][1] = tmp2;
            cube[1][0][2] = tmp3;
        }
    }
    else if (layer_idx == 1) {
        if (dir == 0) {
            tmp1 = cube[5][2][0];
            tmp2 = cube[5][2][1];
            tmp3 = cube[5][2][2];

            cube[5][2][0] = cube[0][2][0];
            cube[5][2][1] = cube[0][2][1];
            cube[5][2][2] = cube[0][2][2];

            cube[0][2][0] = cube[4][2][0];
            cube[0][2][1] = cube[4][2][1];
            cube[0][2][2] = cube[4][2][2];

            cube[4][2][0] = cube[2][0][2];
            cube[4][2][1] = cube[2][0][1];
            cube[4][2][2] = cube[2][0][0];

            cube[2][0][2] = tmp1;
            cube[2][0][1] = tmp2;
            cube[2][0][0] = tmp3;
        }

        else {
            tmp1 = cube[2][0][2];
            tmp2 = cube[2][0][1];
            tmp3 = cube[2][0][0];

            cube[2][0][2] = cube[4][2][0];
            cube[2][0][1] = cube[4][2][1];
            cube[2][0][0] = cube[4][2][2];

            cube[4][2][0] = cube[0][2][0];
            cube[4][2][1] = cube[0][2][1];
            cube[4][2][2] = cube[0][2][2];

            cube[0][2][0] = cube[5][2][0];
            cube[0][2][1] = cube[5][2][1];
            cube[0][2][2] = cube[5][2][2];

            cube[5][2][0] = tmp1;
            cube[5][2][1] = tmp2;
            cube[5][2][2] = tmp3;
        }
    }

    else if (layer_idx == 2) {
        if (dir == 0) {
            tmp1 = cube[3][0][2];
            tmp2 = cube[3][0][1];
            tmp3 = cube[3][0][0];

            cube[3][0][2] = cube[5][2][2];
            cube[3][0][1] = cube[5][1][2];
            cube[3][0][0] = cube[5][0][2];

            cube[5][2][2] = cube[1][2][0];
            cube[5][1][2] = cube[1][2][1];
            cube[5][0][2] = cube[1][2][2];

            cube[1][2][0] = cube[4][0][0];
            cube[1][2][1] = cube[4][1][0];
            cube[1][2][2] = cube[4][2][0];

            cube[4][0][0] = tmp1;
            cube[4][1][0] = tmp2;
            cube[4][2][0] = tmp3;

        }
        else {
            tmp1 = cube[4][0][0];
            tmp2 = cube[4][1][0];
            tmp3 = cube[4][2][0];

            cube[4][0][0] = cube[1][2][0];
            cube[4][1][0] = cube[1][2][1];
            cube[4][2][0] = cube[1][2][2];

            cube[1][2][0] = cube[5][2][2];
            cube[1][2][1] = cube[5][1][2];
            cube[1][2][2] = cube[5][0][2];

            cube[5][2][2] = cube[3][0][2];
            cube[5][1][2] = cube[3][0][1];
            cube[5][0][2] = cube[3][0][0];

            cube[3][0][2] = tmp1;
            cube[3][0][1] = tmp2;
            cube[3][0][0] = tmp3;
        }
    }

    else if (layer_idx == 3) {
        if (dir == 0) {
            tmp1 = cube[5][0][2];
            tmp2 = cube[5][0][1];
            tmp3 = cube[5][0][0];

            cube[5][0][2] = cube[2][2][0];
            cube[5][0][1] = cube[2][2][1];
            cube[5][0][0] = cube[2][2][2];

            cube[2][2][0] = cube[4][0][2];
            cube[2][2][1] = cube[4][0][1];
            cube[2][2][2] = cube[4][0][0];

            cube[4][0][2] = cube[0][0][2];
            cube[4][0][1] = cube[0][0][1];
            cube[4][0][0] = cube[0][0][0];

            cube[0][0][2] = tmp1;
            cube[0][0][1] = tmp2;
            cube[0][0][0] = tmp3;
        }
        else {
            tmp1 = cube[0][0][2];
            tmp2 = cube[0][0][1];
            tmp3 = cube[0][0][0];

            cube[0][0][2] = cube[4][0][2];
            cube[0][0][1] = cube[4][0][1];
            cube[0][0][0] = cube[4][0][0];

            cube[4][0][2] = cube[2][2][0];
            cube[4][0][1] = cube[2][2][1];
            cube[4][0][0] = cube[2][2][2];

            cube[2][2][0] = cube[5][0][2];
            cube[2][2][1] = cube[5][0][1];
            cube[2][2][2] = cube[5][0][0];

            cube[5][0][2] = tmp1;
            cube[5][0][1] = tmp2;
            cube[5][0][0] = tmp3;
        }
    }
    else if (layer_idx == 4) {
        if (dir == 0) {
            tmp1 = cube[0][0][0];
            tmp2 = cube[0][1][0];
            tmp3 = cube[0][2][0];

            cube[0][0][0] = cube[3][0][0];
            cube[0][1][0] = cube[3][1][0];
            cube[0][2][0] = cube[3][2][0];

            cube[3][0][0] = cube[2][0][0];
            cube[3][1][0] = cube[2][1][0];
            cube[3][2][0] = cube[2][2][0];

            cube[2][0][0] = cube[1][0][0];
            cube[2][1][0] = cube[1][1][0];
            cube[2][2][0] = cube[1][2][0];

            cube[1][0][0] = tmp1;
            cube[1][1][0] = tmp2;
            cube[1][2][0] = tmp3;
        }
        else {
            tmp1 = cube[1][0][0];
            tmp2 = cube[1][1][0];
            tmp3 = cube[1][2][0];

            cube[1][0][0] = cube[2][0][0];
            cube[1][1][0] = cube[2][1][0];
            cube[1][2][0] = cube[2][2][0];

            cube[2][0][0] = cube[3][0][0];
            cube[2][1][0] = cube[3][1][0];
            cube[2][2][0] = cube[3][2][0];

            cube[3][0][0] = cube[0][0][0];
            cube[3][1][0] = cube[0][1][0];
            cube[3][2][0] = cube[0][2][0];

            cube[0][0][0] = tmp1;
            cube[0][1][0] = tmp2;
            cube[0][2][0] = tmp3;

        }
    }
    else if (layer_idx == 5) {
        if (dir == 0) {
            tmp1 = cube[0][0][2];
            tmp2 = cube[0][1][2];
            tmp3 = cube[0][2][2];

            cube[0][0][2] = cube[1][0][2];
            cube[0][1][2] = cube[1][1][2];
            cube[0][2][2] = cube[1][2][2];

            cube[1][0][2] = cube[2][0][2];
            cube[1][1][2] = cube[2][1][2];
            cube[1][2][2] = cube[2][2][2];

            cube[2][0][2] = cube[3][0][2];
            cube[2][1][2] = cube[3][1][2];
            cube[2][2][2] = cube[3][2][2];

            cube[3][0][2] = tmp1;
            cube[3][1][2] = tmp2;
            cube[3][2][2] = tmp3;
        }
        else {
            tmp1 = cube[0][0][2];
            tmp2 = cube[0][1][2];
            tmp3 = cube[0][2][2];

            cube[0][0][2] = cube[3][0][2];
            cube[0][1][2] = cube[3][1][2];
            cube[0][2][2] = cube[3][2][2];

            cube[3][0][2] = cube[2][0][2];
            cube[3][1][2] = cube[2][1][2];
            cube[3][2][2] = cube[2][2][2];

            cube[2][0][2] = cube[1][0][2];
            cube[2][1][2] = cube[1][1][2];
            cube[2][2][2] = cube[1][2][2];

            cube[1][0][2] = tmp1;
            cube[1][1][2] = tmp2;
            cube[1][2][2] = tmp3;
        }
    }
}

void turn(char layer, char dir, vector<vector<vector<char>>>& cube) {
    int layer_idx = 0;
    int direction = 0;
    if (layer == 'U') {
        layer_idx = 0;
    }
    else if (layer == 'D') {
        layer_idx = 2;
    }
    else if (layer == 'F') {
        layer_idx = 1;
    }
    else if (layer == 'B') {
        layer_idx = 3;
    }
    else if (layer == 'L') {
        layer_idx = 4;
    }
    else if (layer == 'R') {
        layer_idx = 5;
    }

    if (dir == '+') {
        direction = 0;
    }
    else {
        direction = 1;
    }

    rotate(layer_idx, cube, direction);
    rotate_dimensions(layer_idx, cube, direction);

}
int main() {
    cin >> t;
    while (t--) {
        vector<vector<vector<char>>> cube(6, vector<vector<char>>(3, vector<char>(3)));
        vector<char> colors = { 'w', 'r', 'y', 'o', 'g', 'b' };
        string cmd;
        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] = colors[i];
                }
            }
        }
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> cmd;
            turn(cmd[0], cmd[1], cube);
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                cout << cube[0][i][j];
            }
            cout << '\n';
        }
    }
}