Problem 문제풀이
큐빙을 풀어보지 않은 자. 구현을 논하지 말라.
구현의 끝판왕 큐빙을 풀어보았다. 이번 문제는 특별히 풀이라고 할 것은 없어서
간단한 팁만 남긴다.
전략
-
큐브를 구현할 자료구조(
struct
든, 배열이든 아무거나 상관없다)를 준비한다. -
rotate를 구현하는데 L+, U+, F+를 먼저 구현하는 것을 추천한다.
-
R+는 L-와 유사하게 동작하고 U+는 D-와 F+는 B-와 유사하게 동작한다.
-
+와 -는 순서만 바꿔주면 되기 때문에 간단하다.
-
당신이 L+, U+, F+를 완벽하게 구현했으면 이론적으로 맞았습니다
를 받을 확률이 높다.
그러나 인간은 완벽하지 못하기에 꼭 어디선가 실수를… 읍읍
그럼 Good Luck!
#include <iostream>
#include <string>
using namespace std;
struct Cube {
char up, down, forward, back, left, right;
Cube() {}
Cube(char _up, char _down, char _forward, char _back, char _left, char _right) :up(_up), down(_down), forward(_forward), back(_back), left(_left), right(_right) {}
};
//흰색 : w, 노란색 : y, 빨간색 : r, 오렌지색 : o, 초록색 : g, 파란색 : b
// up, down, forward, back, left, right
int T,n;
Cube cube[3][3][3];
Cube ori[3][3][3] = {
{
{Cube('w','x','x','o','g','x'),Cube('w','x','x','o','x','x'),Cube('w','x','x','o','x','b')}, //3층
{Cube('w','x','x','x','g','x'),Cube('w','x','x','x','x','x'),Cube('w','x','x','x','x','b')},
{Cube('w','x','r','x','g','x'),Cube('w','x','r','x','x','x'),Cube('w','x','r','x','x','b')}
},
{
{Cube('x','x','x','o','g','x'),Cube('x','x','x','o','x','x'),Cube('x','x','x','o','x','b')}, //2층
{Cube('x','x','x','x','g','x'),Cube('x','x','x','x','x','x'),Cube('x','x','x','x','x','b')},
{Cube('x','x','r','x','g','x'),Cube('x','x','r','x','x','x'),Cube('x','x','r','x','x','b')}
},
{
{Cube('x','y','x','o','g','x'),Cube('x','y','x','o','x','x'),Cube('x','y','x','o','x','b')}, //1층
{Cube('x','y','x','x','g','x'),Cube('x','y','x','x','x','x'),Cube('x','y','x','x','x','b')},
{Cube('x','y','r','x','g','x'),Cube('x','y','r','x','x','x'),Cube('x','y','r','x','x','b')}
}
};
void rotate(char face, char dir);
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> T;
for (int tc = 0; tc < T; tc++)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
cube[i][j][k] = ori[i][j][k];
}
}
}
string op;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> op;
rotate(op[0], op[1]);
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << cube[0][i][j].up;
}
cout << '\n';
}
}
return 0;
}
void rotate(char face, char dir)
{
Cube _cube[3][3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
_cube[i][j][k] = cube[i][j][k];
}
}
}
if (face == 'L')
{
if (dir == '+')
{
/*---------------------------------------*/// 000 010 020 <- 200 100 000
_cube[0][0][0].up = cube[2][0][0].back;
_cube[0][0][0].back = cube[2][0][0].down;
_cube[0][0][0].left = cube[2][0][0].left;
_cube[0][1][0].up = cube[1][0][0].back;
_cube[0][1][0].left = cube[1][0][0].left;
_cube[0][2][0].up = cube[0][0][0].back;
_cube[0][2][0].forward = cube[0][0][0].up;
_cube[0][2][0].left = cube[0][0][0].left;
/*---------------------------------------*/// 200 100 <- 220 210
_cube[2][0][0].down = cube[2][2][0].forward;
_cube[2][0][0].back = cube[2][2][0].down;
_cube[2][0][0].left = cube[2][2][0].left;
_cube[1][0][0].back = cube[2][1][0].down;
_cube[1][0][0].left = cube[2][1][0].left;
/*---------------------------------------*/// 220 210 <- 020 120
_cube[2][2][0].forward = cube[0][2][0].up;
_cube[2][2][0].down = cube[0][2][0].forward;
_cube[2][2][0].left = cube[0][2][0].left;
_cube[2][1][0].down = cube[1][2][0].forward;
_cube[2][1][0].left = cube[1][2][0].left;
/*---------------------------------------*/// 120 <- 010
_cube[1][2][0].forward = cube[0][1][0].up;
_cube[1][2][0].left = cube[0][1][0].left;
}
else
{
/*---------------------------------------*/// 000 010 020 -> 200 100 000
_cube[2][0][0].back = cube[0][0][0].up;
_cube[2][0][0].down = cube[0][0][0].back;
_cube[2][0][0].left = cube[0][0][0].left;
_cube[1][0][0].back = cube[0][1][0].up;
_cube[1][0][0].left = cube[0][1][0].left;
_cube[0][0][0].back = cube[0][2][0].up;
_cube[0][0][0].up = cube[0][2][0].forward;
_cube[0][0][0].left = cube[0][2][0].left;
/*---------------------------------------*/// 200 100 -> 220 210
_cube[2][2][0].forward = cube[2][0][0].down;
_cube[2][2][0].down = cube[2][0][0].back;
_cube[2][2][0].left = cube[2][0][0].left;
_cube[2][1][0].down = cube[1][0][0].back;
_cube[2][1][0].left = cube[1][0][0].left;
/*---------------------------------------*/// 220 210 -> 020 120
_cube[0][2][0].up = cube[2][2][0].forward;
_cube[0][2][0].forward = cube[2][2][0].down;
_cube[0][2][0].left = cube[2][2][0].left;
_cube[1][2][0].forward = cube[2][1][0].down;
_cube[1][2][0].left = cube[2][1][0].left;
/*---------------------------------------*/// 120 -> 010
_cube[0][1][0].up = cube[1][2][0].forward;
_cube[0][1][0].left = cube[1][2][0].left;
}
}
else if (face == 'R')
{
if (dir == '+')
{
/*---------------------------------------*/// 002 012 022 -> 202 102 002
_cube[2][0][2].back = cube[0][0][2].up;
_cube[2][0][2].down = cube[0][0][2].back;
_cube[2][0][2].right = cube[0][0][2].right;
_cube[1][0][2].back = cube[0][1][2].up;
_cube[1][0][2].right = cube[0][1][2].right;
_cube[0][0][2].back = cube[0][2][2].up;
_cube[0][0][2].up = cube[0][2][2].forward;
_cube[0][0][2].right = cube[0][2][2].right;
/*---------------------------------------*/// 202 102 -> 222 212
_cube[2][2][2].forward = cube[2][0][2].down;
_cube[2][2][2].down = cube[2][0][2].back;
_cube[2][2][2].right = cube[2][0][2].right;
_cube[2][1][2].down = cube[1][0][2].back;
_cube[2][1][2].right = cube[1][0][2].right;
/*---------------------------------------*/// 222 212 -> 022 122
_cube[0][2][2].up = cube[2][2][2].forward;
_cube[0][2][2].forward = cube[2][2][2].down;
_cube[0][2][2].right = cube[2][2][2].right;
_cube[1][2][2].forward = cube[2][1][2].down;
_cube[1][2][2].right = cube[2][1][2].right;
/*---------------------------------------*/// 122 -> 012
_cube[0][1][2].up = cube[1][2][2].forward;
_cube[0][1][2].right = cube[1][2][2].right;
}
else
{
/*---------------------------------------*/// 002 012 022 <- 202 102 002
_cube[0][0][2].up = cube[2][0][2].back;
_cube[0][0][2].back = cube[2][0][2].down;
_cube[0][0][2].right = cube[2][0][2].right;
_cube[0][1][2].up = cube[1][0][2].back;
_cube[0][1][2].right = cube[1][0][2].right;
_cube[0][2][2].up = cube[0][0][2].back;
_cube[0][2][2].forward = cube[0][0][2].up;
_cube[0][2][2].right = cube[0][0][2].right;
/*---------------------------------------*/// 202 102 <- 222 212
_cube[2][0][2].down = cube[2][2][2].forward;
_cube[2][0][2].back = cube[2][2][2].down;
_cube[2][0][2].right = cube[2][2][2].right;
_cube[1][0][2].back = cube[2][1][2].down;
_cube[1][0][2].right = cube[2][1][2].right;
/*---------------------------------------*/// 222 212 <- 022 122
_cube[2][2][2].forward = cube[0][2][2].up;
_cube[2][2][2].down = cube[0][2][2].forward;
_cube[2][2][2].right = cube[0][2][2].right;
_cube[2][1][2].down = cube[1][2][2].forward;
_cube[2][1][2].right = cube[1][2][2].right;
/*---------------------------------------*/// 122 <- 012
_cube[1][2][2].forward = cube[0][1][2].up;
_cube[1][2][2].right = cube[0][1][2].right;
}
}
else if (face == 'F')
{
if (dir == '+')
{
/*---------------------------------------*/// 020 021 022 -> 022 122 222
_cube[0][2][2].up = cube[0][2][0].left;
_cube[0][2][2].right = cube[0][2][0].up;
_cube[0][2][2].forward = cube[0][2][0].forward;
_cube[1][2][2].right = cube[0][2][1].up;
_cube[1][2][2].forward = cube[0][2][1].forward;
_cube[2][2][2].right = cube[0][2][2].up;
_cube[2][2][2].down = cube[0][2][2].right;
_cube[2][2][2].forward = cube[0][2][2].forward;
/*---------------------------------------*/// 122 222 -> 221 220
_cube[2][2][1].down = cube[1][2][2].right;
_cube[2][2][1].forward = cube[1][2][2].forward;
_cube[2][2][0].down = cube[2][2][2].right;
_cube[2][2][0].left = cube[2][2][2].down;
_cube[2][2][0].forward = cube[2][2][2].forward;
/*---------------------------------------*/// 221 220 -> 120 020
_cube[1][2][0].left = cube[2][2][1].down;
_cube[1][2][0].forward = cube[2][2][1].forward;
_cube[0][2][0].up = cube[2][2][0].left;
_cube[0][2][0].left = cube[2][2][0].down;
_cube[0][2][0].forward = cube[2][2][0].forward;
/*---------------------------------------*/// 120 -> 021
_cube[0][2][1].up = cube[1][2][0].left;
_cube[0][2][1].forward = cube[1][2][0].forward;
}
else
{
/*---------------------------------------*/// 020 021 022 <- 022 122 222
_cube[0][2][0].left = cube[0][2][2].up;
_cube[0][2][0].up = cube[0][2][2].right;
_cube[0][2][0].forward = cube[0][2][2].forward;
_cube[0][2][1].up = cube[1][2][2].right;
_cube[0][2][1].forward = cube[1][2][2].forward;
_cube[0][2][2].up = cube[2][2][2].right;
_cube[0][2][2].right = cube[2][2][2].down;
_cube[0][2][2].forward = cube[2][2][2].forward;
/*---------------------------------------*/// 122 222 <- 221 220
_cube[1][2][2].right = cube[2][2][1].down;
_cube[1][2][2].forward = cube[2][2][1].forward;
_cube[2][2][2].right = cube[2][2][0].down;
_cube[2][2][2].down = cube[2][2][0].left;
_cube[2][2][2].forward = cube[2][2][0].forward;
/*---------------------------------------*/// 221 220 <- 120 020
_cube[2][2][1].down = cube[1][2][0].left;
_cube[2][2][1].forward = cube[1][2][0].forward;
_cube[2][2][0].left = cube[0][2][0].up;
_cube[2][2][0].down = cube[0][2][0].left;
_cube[2][2][0].forward = cube[0][2][0].forward;
/*---------------------------------------*/// 120 <- 021
_cube[1][2][0].left = cube[0][2][1].up;
_cube[1][2][0].forward = cube[0][2][1].forward;
}
}
else if (face == 'B')
{
if (dir == '+')
{
/*---------------------------------------*/// 000 001 002 <- 002 102 202
_cube[0][0][0].left = cube[0][0][2].up;
_cube[0][0][0].up = cube[0][0][2].right;
_cube[0][0][0].back = cube[0][0][2].back;
_cube[0][0][1].up = cube[1][0][2].right;
_cube[0][0][1].back = cube[1][0][2].back;
_cube[0][0][2].up = cube[2][0][2].right;
_cube[0][0][2].right = cube[2][0][2].down;
_cube[0][0][2].back = cube[2][0][2].back;
/*---------------------------------------*/// 102 202 <- 201 200
_cube[1][0][2].right = cube[2][0][1].down;
_cube[1][0][2].back = cube[2][0][1].back;
_cube[2][0][2].right = cube[2][0][0].down;
_cube[2][0][2].down = cube[2][0][0].left;
_cube[2][0][2].back = cube[2][0][0].back;
/*---------------------------------------*/// 201 200 <- 100 000
_cube[2][0][1].down = cube[1][0][0].left;
_cube[2][0][1].back = cube[1][0][0].back;
_cube[2][0][0].left = cube[0][0][0].up;
_cube[2][0][0].down = cube[0][0][0].left;
_cube[2][0][0].back = cube[0][0][0].back;
/*---------------------------------------*/// 100 <- 001
_cube[1][0][0].left = cube[0][0][1].up;
_cube[1][0][0].back = cube[0][0][1].back;
}
else
{
/*---------------------------------------*/// 000 001 002 -> 002 102 202
_cube[0][0][2].up = cube[0][0][0].left;
_cube[0][0][2].right = cube[0][0][0].up;
_cube[0][0][2].back = cube[0][0][0].back;
_cube[1][0][2].right = cube[0][0][1].up;
_cube[1][0][2].back = cube[0][0][1].back;
_cube[2][0][2].right = cube[0][0][2].up;
_cube[2][0][2].down = cube[0][0][2].right;
_cube[2][0][2].back = cube[0][0][2].back;
/*---------------------------------------*/// 102 202 -> 201 200
_cube[2][0][1].down = cube[1][0][2].right;
_cube[2][0][1].back = cube[1][0][2].back;
_cube[2][0][0].down = cube[2][0][2].right;
_cube[2][0][0].left = cube[2][0][2].down;
_cube[2][0][0].back = cube[2][0][2].back;
/*---------------------------------------*/// 201 200 -> 100 000
_cube[1][0][0].left = cube[2][0][1].down;
_cube[1][0][0].back = cube[2][0][1].back;
_cube[0][0][0].up = cube[2][0][0].left;
_cube[0][0][0].left = cube[2][0][0].down;
_cube[0][0][0].back = cube[2][0][0].back;
/*---------------------------------------*/// 100 -> 001
_cube[0][0][1].up = cube[1][0][0].left;
_cube[0][0][1].back = cube[1][0][0].back;
}
}
else if (face == 'U')
{
if (dir == '+')
{
/*---------------------------------------*/// 000 001 002 <- 020 010 000
_cube[0][0][0].back = cube[0][2][0].left;
_cube[0][0][0].left = cube[0][2][0].forward;
_cube[0][0][0].up = cube[0][2][0].up;
_cube[0][0][1].back = cube[0][1][0].left;
_cube[0][0][1].up = cube[0][1][0].up;
_cube[0][0][2].back = cube[0][0][0].left;
_cube[0][0][2].right = cube[0][0][0].back;
_cube[0][0][2].up = cube[0][0][0].up;
/*---------------------------------------*/// 012 022 <- 001 002
_cube[0][1][2].right = cube[0][0][1].back;
_cube[0][1][2].up = cube[0][0][1].up;
_cube[0][2][2].right = cube[0][0][2].back;
_cube[0][2][2].forward = cube[0][0][2].right;
_cube[0][2][2].up = cube[0][0][2].up;
/*---------------------------------------*/// 021 020 <- 012 022
_cube[0][2][1].forward = cube[0][1][2].right;
_cube[0][2][1].up = cube[0][1][2].up;
_cube[0][2][0].forward = cube[0][2][2].right;
_cube[0][2][0].left = cube[0][2][2].forward;
_cube[0][2][0].up = cube[0][2][2].up;
/*---------------------------------------*/// 010 <- 021
_cube[0][1][0].left = cube[0][2][1].forward;
_cube[0][1][0].up = cube[0][2][1].up;
}
else
{
/*---------------------------------------*/// 000 001 002 -> 020 010 000
_cube[0][2][0].left = cube[0][0][0].back;
_cube[0][2][0].forward = cube[0][0][0].left;
_cube[0][2][0].up = cube[0][0][0].up;
_cube[0][1][0].left = cube[0][0][1].back;
_cube[0][1][0].up = cube[0][0][1].up;
_cube[0][0][0].left = cube[0][0][2].back;
_cube[0][0][0].back = cube[0][0][2].right;
_cube[0][0][0].up = cube[0][0][2].up;
/*---------------------------------------*/// 012 022 -> 001 002
_cube[0][0][1].back = cube[0][1][2].right;
_cube[0][0][1].up = cube[0][1][2].up;
_cube[0][0][2].back = cube[0][2][2].right;
_cube[0][0][2].right = cube[0][2][2].forward;
_cube[0][0][2].up = cube[0][2][2].up;
/*---------------------------------------*/// 021 020 -> 012 022
_cube[0][1][2].right = cube[0][2][1].forward;
_cube[0][1][2].up = cube[0][2][1].up;
_cube[0][2][2].right = cube[0][2][0].forward;
_cube[0][2][2].forward = cube[0][2][0].left;
_cube[0][2][2].up = cube[0][2][0].up;
/*---------------------------------------*/// 010 -> 021
_cube[0][2][1].forward = cube[0][1][0].left;
_cube[0][2][1].up = cube[0][1][0].up;
}
}
else if (face == 'D')
{
if (dir == '+')
{
/*---------------------------------------*/// 200 201 202 <- 220 210 200
_cube[2][2][0].left = cube[2][0][0].back;
_cube[2][2][0].forward = cube[2][0][0].left;
_cube[2][2][0].down = cube[2][0][0].down;
_cube[2][1][0].left = cube[2][0][1].back;
_cube[2][1][0].down = cube[2][0][1].down;
_cube[2][0][0].left = cube[2][0][2].back;
_cube[2][0][0].back = cube[2][0][2].right;
_cube[2][0][0].down = cube[2][0][2].down;
/*---------------------------------------*/// 212 222 <- 201 202
_cube[2][0][1].back = cube[2][1][2].right;
_cube[2][0][1].down = cube[2][1][2].down;
_cube[2][0][2].back = cube[2][2][2].right;
_cube[2][0][2].right = cube[2][2][2].forward;
_cube[2][0][2].down = cube[2][2][2].down;
/*---------------------------------------*/// 221 220 <- 212 222
_cube[2][1][2].right = cube[2][2][1].forward;
_cube[2][1][2].down = cube[2][2][1].down;
_cube[2][2][2].right = cube[2][2][0].forward;
_cube[2][2][2].forward = cube[2][2][0].left;
_cube[2][2][2].down = cube[2][2][0].down;
/*---------------------------------------*/// 210 <- 221
_cube[2][2][1].forward = cube[2][1][0].left;
_cube[2][2][1].down = cube[2][1][0].down;
}
else
{
/*---------------------------------------*/// 200 201 202 <- 220 210 200
_cube[2][0][0].back = cube[2][2][0].left;
_cube[2][0][0].left = cube[2][2][0].forward;
_cube[2][0][0].down = cube[2][2][0].down;
_cube[2][0][1].back = cube[2][1][0].left;
_cube[2][0][1].down = cube[2][1][0].down;
_cube[2][0][2].back = cube[2][0][0].left;
_cube[2][0][2].right = cube[2][0][0].back;
_cube[2][0][2].down = cube[2][0][0].down;
/*---------------------------------------*/// 212 222 <- 201 202
_cube[2][1][2].right = cube[2][0][1].back;
_cube[2][1][2].down = cube[2][0][1].down;
_cube[2][2][2].right = cube[2][0][2].back;
_cube[2][2][2].forward = cube[2][0][2].right;
_cube[2][2][2].down = cube[2][0][2].down;
/*---------------------------------------*/// 221 220 <- 212 222
_cube[2][2][1].forward = cube[2][1][2].right;
_cube[2][2][1].down = cube[2][1][2].down;
_cube[2][2][0].forward = cube[2][2][2].right;
_cube[2][2][0].left = cube[2][2][2].forward;
_cube[2][2][0].down = cube[2][2][2].down;
/*---------------------------------------*/// 210 <- 221
_cube[2][1][0].left = cube[2][2][1].forward;
_cube[2][1][0].down = cube[2][2][1].down;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
cube[i][j][k] = _cube[i][j][k];
}
}
}
}