14891번: 톱니바퀴
총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴
www.acmicpc.net
- deque로 톱니바퀴 세팅
- 왼쪽 오른쪽으로 맞물리는 곳이 다른지 확인
- 기준과 인덱스 차이가 짝수면 같은방향 회전, 다르면 다른방향 회전
- 2의 제곱수로 결과 계산
#14891 톱니바퀴
import sys
from collections import deque
input = sys.stdin.readline
wheel = []
for _ in range(4):
wheel.append(deque(input().rstrip()))
rotate_num = int(input())
rotate_list = []
for _ in range(rotate_num):
rotate_list.append(list(map(int, input().split())))
def rotate(standard, num, direction):
if abs(standard - num) % 2 == 1:
direction = -1*direction
if direction == 1:
temp = wheel[num].pop()
wheel[num].insert(0, temp)
else:
temp = wheel[num].popleft()
wheel[num].append(temp)
for num, direction in rotate_list:
num -= 1
left = num-1
right = num+1
while(True):
if 0 <= left and wheel[left+1][6] != wheel[left][2]:
left -= 1
else:
break
while(True):
if right < 4 and wheel[right-1][2] != wheel[right][6]:
right += 1
else:
break
for i in range(left+1, right):
rotate(num, i, direction)
answer = 0
for i in range(4):
if wheel[i][0] == '1':
answer += pow(2, i)
print(answer)
'개발 > 알고리즘' 카테고리의 다른 글
| [백준 11723] 집합 (python) (0) | 2021.03.18 |
|---|---|
| [백준 1541] 잃어버린 괄호 (python) (0) | 2021.03.18 |
| [백준 1074] Z (python) (0) | 2021.03.17 |
| [백준 14888] 연산자 끼워넣기 (python) (0) | 2021.03.17 |
| [백준 17144] 미세먼지 안녕! (python) (0) | 2021.03.17 |




최근댓글