취준/백준
[백준][Python][14891][시뮬레이션] 톱니바퀴
puff
2020. 2. 27. 18:13
문제 : https://www.acmicpc.net/problem/14891
방향 헷갈리지 말자.
[1,2,3,4,5,6,7,8] 일때
시계방향 -> [8,1,2,3,4,5,6,7]
반시계방향 -> [2,3,4,5,6,7,8,1]
import sys
t = []
for _ in range(4):
a = sys.stdin.readline().rstrip()
t.append([int(a[i]) for i in range(len(a))])
cmd = []
n = int(input())
for _ in range(n):
cmd.append([int(i) for i in sys.stdin.readline().split() ])
def rotate(lst, clockwise):
if clockwise:
return [lst[-1]] + lst[0:-1]
else:
return lst[1:] + [lst[0]]
for c in cmd:
direct = [0]*4
no = c[0]-1
if c[1] ==1:
direct[no] = 1
else:
direct[no] = -1
for i in reversed(range(no)):
if t[i][2] != t[i+1][6]:
direct[i] = (-1)*direct[i+1]
else:
break
for i in range(no+1,4):
if t[i-1][2] != t[i][6]:
direct[i] = (-1)*direct[i-1]
else:
break
for i in range(4):
if direct[i] ==0:
continue
elif direct[i] == 1:
t[i] = rotate(t[i],True)
elif direct[i] == -1:
t[i] = rotate(t[i],False)
print(t[0][0]*1 + t[1][0]*2 + t[2][0]*4 + t[3][0]*8)