https://www.acmicpc.net/problem/5373
큐빙 문제를 풀기전에 아래 문제를 먼저 풀어보자.
또한 큐빙 문제를 디버깅하는 동안 아래 사이트를 이용하면 이해하는데 도움이 된다.
https://rubiks-cube-solver.com/ko/
아이디어는
- Step1. 기준점 정하고 3차원 배열 생성 (6 X 3 X 3)
- Step2. 면 회전 구현
- Step3. 기준면과 인접한 면의 이동 구현
- Step4. 반시계방향은 시계방향을 3번 반복한 것과 같음을 이용
def clockwise(arr): # 시계방향으로 k번 만큼 회전
tmp = arr[0][0]
arr[0][0] = arr[2][0]
arr[2][0] = arr[2][2]
arr[2][2] = arr[0][2]
arr[0][2] = tmp
tmp = arr[0][1]
arr[0][1] = arr[1][0]
arr[1][0] = arr[2][1]
arr[2][1] = arr[1][2]
arr[1][2] = tmp
def U(c):
if c == '+':
k = 1
else:
k = 3
for _ in range(k):
clockwise(cube[0])
tmp = cube[1][0][0], cube[1][0][1], cube[1][0][2]
cube[1][0][0], cube[1][0][1], cube[1][0][2] = cube[4][2][0], cube[4][1][0], cube[4][0][0]
cube[4][2][0], cube[4][1][0], cube[4][0][0] = cube[5][2][2], cube[5][2][1], cube[5][2][0]
cube[5][2][2], cube[5][2][1], cube[5][2][0] = cube[3][0][2], cube[3][1][2], cube[3][2][2]
cube[3][0][2], cube[3][1][2], cube[3][2][2] = tmp
def D(c):
if c == '+':
k = 1
else:
k = 3
for _ in range(k):
clockwise(cube[2])
tmp = cube[4][0][2], cube[4][1][2], cube[4][2][2]
cube[4][0][2], cube[4][1][2], cube[4][2][2] = cube[1][2][2], cube[1][2][1], cube[1][2][0]
cube[1][2][2], cube[1][2][1], cube[1][2][0] = cube[3][2][0], cube[3][1][0], cube[3][0][0]
cube[3][2][0], cube[3][1][0], cube[3][0][0] = cube[5][0][0], cube[5][0][1], cube[5][0][2]
cube[5][0][0], cube[5][0][1], cube[5][0][2] = tmp
def R(c):
if c == '+':
k = 1
else:
k = 3
for _ in range(k):
clockwise(cube[4])
tmp = cube[0][0][2], cube[0][1][2], cube[0][2][2]
cube[0][0][2], cube[0][1][2], cube[0][2][2] = cube[1][0][2], cube[1][1][2], cube[1][2][2]
cube[1][0][2], cube[1][1][2], cube[1][2][2] = cube[2][0][2], cube[2][1][2], cube[2][2][2]
cube[2][0][2], cube[2][1][2], cube[2][2][2] = cube[5][0][2], cube[5][1][2], cube[5][2][2]
cube[5][0][2], cube[5][1][2], cube[5][2][2] = tmp
def L(c):
if c == '+':
k = 1
else:
k = 3
for _ in range(k):
clockwise(cube[3])
tmp = cube[0][0][0], cube[0][1][0], cube[0][2][0]
cube[0][0][0], cube[0][1][0], cube[0][2][0] = cube[5][0][0], cube[5][1][0], cube[5][2][0]
cube[5][0][0], cube[5][1][0], cube[5][2][0] = cube[2][0][0], cube[2][1][0], cube[2][2][0]
cube[2][0][0], cube[2][1][0], cube[2][2][0] = cube[1][0][0], cube[1][1][0], cube[1][2][0]
cube[1][0][0], cube[1][1][0], cube[1][2][0] = tmp
def F(c):
if c == '+':
k = 1
else:
k = 3
for _ in range(k):
clockwise(cube[1])
tmp = cube[0][2][0], cube[0][2][1], cube[0][2][2]
cube[0][2][0], cube[0][2][1], cube[0][2][2] = cube[3][2][0], cube[3][2][1], cube[3][2][2]
cube[3][2][0], cube[3][2][1], cube[3][2][2] = cube[2][0][2], cube[2][0][1], cube[2][0][0]
cube[2][0][2], cube[2][0][1], cube[2][0][0] = cube[4][2][0], cube[4][2][1], cube[4][2][2]
cube[4][2][0], cube[4][2][1], cube[4][2][2] = tmp
def B(c):
if c == '+':
k = 1
else:
k = 3
for _ in range(k):
clockwise(cube[5])
tmp = cube[0][0][0], cube[0][0][1], cube[0][0][2]
cube[0][0][0], cube[0][0][1], cube[0][0][2] = cube[4][0][0], cube[4][0][1], cube[4][0][2]
cube[4][0][0], cube[4][0][1], cube[4][0][2] = cube[2][2][2], cube[2][2][1], cube[2][2][0]
cube[2][2][2], cube[2][2][1], cube[2][2][0] = cube[3][0][0], cube[3][0][1], cube[3][0][2]
cube[3][0][0], cube[3][0][1], cube[3][0][2] = tmp
from copy import deepcopy
CUBE = [[[] for _ in range(3)]for _ in range(6)]
# 큐브 초기화
s = 'wrygbo' # U F D L R B
for i in range(6):
for j in range(3):
for _ in range(3):
CUBE[i][j].append(s[i])
for _ in range(int(input())):
n = int(input())
cmd = input().split()
cube = deepcopy(CUBE)
while cmd:
c = cmd.pop(0)
if c[0] == 'L':
L(c[1])
elif c[0] == 'D':
D(c[1])
elif c[0] == 'U':
U(c[1])
elif c[0] == 'F':
F(c[1])
elif c[0] == 'R':
R(c[1])
elif c[0] == 'B':
B(c[1])
for i in range(3):
print(''.join(cube[0][i]))
728x90
반응형
'Problem Solving > BOJ' 카테고리의 다른 글
[Python]17070. 파이프 옮기기1 (0) | 2019.04.12 |
---|---|
[Python]16918. 봄버맨 (0) | 2019.04.11 |
[Python]16939. 2x2x2 큐브 (0) | 2019.04.10 |
[Python]16235. 나무 재테크 (0) | 2019.04.10 |
[Python]16236. 아기 상어 (0) | 2019.04.09 |
댓글