From a233610517871a98491d49bed470db04711caf99 Mon Sep 17 00:00:00 2001 From: bong-u Date: Wed, 21 Dec 2022 21:59:42 +0900 Subject: [PATCH] #14891 --- .../14891.py" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "\354\206\220\353\264\211\354\232\260/14891.py" diff --git "a/\354\206\220\353\264\211\354\232\260/14891.py" "b/\354\206\220\353\264\211\354\232\260/14891.py" new file mode 100644 index 00000000..f1c6da14 --- /dev/null +++ "b/\354\206\220\353\264\211\354\232\260/14891.py" @@ -0,0 +1,40 @@ +from collections import deque + +wheels = [deque(map(int, list(input()))) for _ in range(4)] + +def rotate(wheel, direction): + if direction == 1: + wheel.appendleft(wheel.pop()) + else: + wheel.append(wheel.popleft()) + return wheel + +K = int(input()) +vd = [1, -1] +res = 0 + +for _ in range(K): + N, D = map(int, input().split()) + q = deque() + q.append((N-1, D)) + visited = [False] * 4 + tmp = [] + + while q: + cur, curD = q.popleft() + tmp.append((cur, curD)) + visited[cur] = True + + for v in vd: + nxt = cur+v + if 0 <= nxt < 4 and not visited[nxt]: + if (v == -1 and wheels[cur][6] != wheels[nxt][2] + or v == 1 and wheels[nxt][6] != wheels[cur][2]): + q.append((nxt, -1 if curD == 1 else 1)) + visited[nxt] = True + for cur, curD in tmp: + rotate(wheels[cur], curD) + +for i in range(4): + res += wheels[i][0]*(pow(2, i)) +print (res) \ No newline at end of file