Baekjoon-5373-큐빙

진짜 쌩 구현….
푸는데 진짜 오래 걸렸다.


class Cube:
def __init__(self):
self.top = self.initialize('w')
self.bottom = self.initialize('y')
self.front = self.initialize('r')
self.back = self.initialize('o')
self.left = self.initialize('g')
self.right = self.initialize('b')

def initialize(self, color):
return [[color]*3 for _ in range(3)]

def rot(self, side, dirs):
new_ = [[0]*3 for _ in range(3)]
if dirs == 1: # CCW
a,b,c,d = 0,1,2,-1
else: # CW
a,b,c,d = 2,-1,0,1
for i in range(3):
for j in range(3):
new_[i][j] = getattr(self,side)[a+b*j][c+d*i]
setattr(self, side, new_)

def print(self):
for line in self.top:
print(*line,sep='')

def turn(self, side, dirs):
if side == 'U':
if dirs == '+':
self.rot('top',-1)
self.left[0], self.front[0], self.right[0], self.back[0] = \
self.front[0], self.right[0], self.back[0], self.left[0]
else:
self.rot('top',1)
self.left[0], self.front[0], self.right[0], self.back[0] = \
self.back[0], self.left[0], self.front[0], self.right[0]
elif side == 'D':
if dirs == '+':
self.rot('bottom',-1)
self.left[2], self.front[2], self.right[2], self.back[2] = \
self.back[2], self.left[2], self.front[2], self.right[2]
else:
self.rot('bottom',1)
self.left[2], self.front[2], self.right[2], self.back[2] = \
self.front[2], self.right[2], self.back[2], self.left[2]
elif side == 'F':
if dirs == '+':
self.rot('front',-1)
self.top[2], \
self.left[0][2], self.left[1][2], self.left[2][2],\
self.bottom[2], \
self.right[0][0], self.right[1][0], self.right[2][0] = \
[self.left[2][2], self.left[1][2], self.left[0][2]],\
*self.bottom[2][::-1], \
[self.right[0][0], self.right[1][0], self.right[2][0]],\
*self.top[2]
else:
self.rot('front',1)
self.top[2], \
self.left[0][2], self.left[1][2], self.left[2][2],\
self.bottom[2], \
self.right[0][0], self.right[1][0], self.right[2][0] = \
[self.right[0][0], self.right[1][0], self.right[2][0]],\
*self.top[2][::-1],\
[self.left[2][2], self.left[1][2], self.left[0][2]],\
*self.bottom[2]
elif side == 'B':
if dirs == '+':
self.rot('back',-1)
self.top[0], \
self.left[0][0], self.left[1][0], self.left[2][0],\
self.bottom[0], \
self.right[0][2], self.right[1][2], self.right[2][2] = \
[self.right[0][2], self.right[1][2], self.right[2][2]],\
*self.top[0][::-1], \
[self.left[2][0], self.left[1][0], self.left[0][0]],\
*self.bottom[0]
else:
self.rot('back',1)
self.top[0], \
self.left[0][0], self.left[1][0], self.left[2][0],\
self.bottom[0], \
self.right[0][2], self.right[1][2], self.right[2][2] = \
[self.left[2][0], self.left[1][0], self.left[0][0]],\
*self.bottom[0][::-1],\
[self.right[0][2], self.right[1][2], self.right[2][2]],\
*self.top[0]
elif side == 'L':
if dirs == '+':
self.rot('left',-1)
self.top[0][0],self.top[1][0],self.top[2][0], \
self.back[0][2],self.back[1][2],self.back[2][2],\
self.bottom[0][2],self.bottom[1][2],self.bottom[2][2], \
self.front[0][0],self.front[1][0],self.front[2][0] = \
self.back[2][2],self.back[1][2],self.back[0][2],\
self.bottom[0][2],self.bottom[1][2],self.bottom[2][2], \
self.front[2][0],self.front[1][0],self.front[0][0],\
self.top[0][0],self.top[1][0],self.top[2][0]
else:
self.rot('left',1)
self.top[0][0],self.top[1][0],self.top[2][0], \
self.back[0][2],self.back[1][2],self.back[2][2],\
self.bottom[0][2],self.bottom[1][2],self.bottom[2][2], \
self.front[0][0],self.front[1][0],self.front[2][0] = \
self.front[0][0],self.front[1][0],self.front[2][0],\
self.top[2][0],self.top[1][0],self.top[0][0], \
self.back[0][2],self.back[1][2],self.back[2][2],\
self.bottom[2][2],self.bottom[1][2],self.bottom[0][2]
elif side == 'R':
if dirs == '+':
self.rot('right',-1)
self.top[0][2],self.top[1][2],self.top[2][2], \
self.back[0][0],self.back[1][0],self.back[2][0],\
self.bottom[0][0],self.bottom[1][0],self.bottom[2][0], \
self.front[0][2],self.front[1][2],self.front[2][2] = \
self.front[0][2],self.front[1][2],self.front[2][2], \
self.top[2][2],self.top[1][2],self.top[0][2], \
self.back[0][0],self.back[1][0],self.back[2][0],\
self.bottom[2][0],self.bottom[1][0],self.bottom[0][0]

else:
self.rot('right',1)
self.top[0][2],self.top[1][2],self.top[2][2], \
self.back[0][0],self.back[1][0],self.back[2][0],\
self.bottom[0][0],self.bottom[1][0],self.bottom[2][0], \
self.front[0][2],self.front[1][2],self.front[2][2] = \
self.back[2][0],self.back[1][0],self.back[0][0],\
self.bottom[0][0],self.bottom[1][0],self.bottom[2][0],\
self.front[2][2],self.front[1][2],self.front[0][2], \
self.top[0][2],self.top[1][2],self.top[2][2]

T = int(input())
result = []
for _ in range(T):
cube = Cube()
n = int(input())
for side,dirs in input().split():
cube.turn(side,dirs)
result.append(cube.top)

for r in result:
for line in r:
print(*line,sep='')