백준 - 14891 : 톱니바퀴 (G5)

수정일 : 2024-11-15

 1from collections import deque
 2
 3wheels = [deque(map(int, list(input()))) for _ in range(4)]
 4
 5def rotate(wheel, direction):
 6    if direction == 1:
 7        wheel.appendleft(wheel.pop())
 8    else:
 9        wheel.append(wheel.popleft())
10    return wheel
11
12K = int(input())
13vd = [1, -1]
14res = 0
15
16for _ in range(K):
17    N, D = map(int, input().split())
18    q = deque()
19    q.append((N-1, D))
20    visited = [False] * 4
21    tmp = []
22
23    while q:
24        cur, curD = q.popleft()
25        tmp.append((cur, curD))
26        visited[cur] = True
27
28        for v in vd:
29            nxt = cur+v
30            if 0 <= nxt < 4 and not visited[nxt]:
31                if (v == -1 and wheels[cur][6] != wheels[nxt][2]
32                or v == 1 and wheels[nxt][6] != wheels[cur][2]):
33                    q.append((nxt, -1 if curD == 1 else 1))
34                    visited[nxt] = True
35    for cur, curD in tmp:
36        rotate(wheels[cur], curD)
37
38for i in range(4):
39    res += wheels[i][0]*(pow(2, i))
40print (res)
  • 문제를 제대로 이해하는데 오래걸렸다
  • 혼자 힘으로 풀어서 뿌듯하다
  • 구현문제 많이 풀어보자!