IT/coding study

[acmicpc] 14891. 톱니바퀴(python)

seyeonHello 2022. 9. 1. 22:11

시뮬레이션

import sys
from collections import deque
input=sys.stdin.readline

wheels=[[]]
for i in range(4):
    wheels.append(deque(list(input().strip())))
K=int(input())
for i in range(K):
    num, dir=map(int,input().split())
    moves = [0] * 5 # 회전할 톱니바퀴 방향 체크
    moves[num] = dir
    left=wheels[num][6]
    for j in range(num-1,0,-1): # 맨왼쪽까지 비교
        if wheels[j][2]==left: break # 극이 같으면 stop
        moves[j]=-moves[j+1]
        left=wheels[j][6]
    right=wheels[num][2]
    for j in range(num+1,5): # 맨오른쪽까지 비교
        if wheels[j][6]==right: break # 극이 같으면 stop
        moves[j]=-moves[j-1]
        right=wheels[j][2]
    for j in range(5):
        if moves[j]!=0:
            wheels[j].rotate(moves[j]) # 회전시키기
point=0
for i in range(1,5):
    if wheels[i][0]=='1':
        point+=pow(2,i-1)
print(point)

 


https://www.acmicpc.net/problem/14891

 

14891번: 톱니바퀴

총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴

www.acmicpc.net