Skip to content

Commit 24a1af2

Browse files
committed
[BOJ] 14891 톱니바퀴 (G5)
1 parent a7f5aa3 commit 24a1af2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

최어진/9주차/260224.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 백준 14891번: 톱니바퀴
2+
3+
import sys
4+
5+
input = sys.stdin.readline
6+
gears = [list(map(int, list(input().rstrip()))) for _ in range(4)]
7+
# K <= 10^2
8+
K = int(input())
9+
10+
# 1. 총 K번 회전 -> 10^2
11+
# 2. 각 회전마다 최대 4개까지 톱니바퀴가 따라서 회전 가능 ~= 10
12+
# 결론. 10^5 내에 통과
13+
14+
visited = None
15+
LEFT = 6
16+
RIGHT = 2
17+
18+
def print_gears():
19+
for gear in gears:
20+
print(gear)
21+
print()
22+
23+
def spin(idx, direction):
24+
# print(f'spin({idx}, {direction})')
25+
# 왼쪽 기어 체크
26+
if idx - 1 >= 0 and not visited[idx - 1] and gears[idx - 1][RIGHT] != gears[idx][LEFT]:
27+
visited[idx - 1] = True
28+
spin(idx - 1, direction * -1)
29+
# 오른쪽 기어 체크
30+
if idx + 1 <= 3 and not visited[idx + 1] and gears[idx][RIGHT] != gears[idx + 1][LEFT]:
31+
visited[idx + 1] = True
32+
spin(idx + 1, direction * -1)
33+
34+
if direction == 1:
35+
gears[idx] = gears[idx][-1:] + gears[idx][:-1]
36+
else:
37+
gears[idx] = gears[idx][1:] + gears[idx][:1]
38+
39+
def get_score():
40+
score = 0
41+
for idx in range(4):
42+
if gears[idx][0]: score += (2 ** idx)
43+
return score
44+
45+
for _ in range(K):
46+
gear_idx, gear_direction = map(int, input().rstrip().split())
47+
# gear_direction: 1이면 시계 방향, -1이면 반시계 방향
48+
# print(gear_idx, gear_direction)
49+
50+
visited = [False for _ in range(4)]
51+
visited[gear_idx - 1] = True
52+
spin(gear_idx - 1, gear_direction)
53+
54+
# print_gears()
55+
56+
print(get_score())

0 commit comments

Comments
 (0)