본문 바로가기

알고리즘/백준

[백준] 20055번 : 컨베이어 벨트 위의 로봇

728x90

20055번: 컨베이어 벨트 위의 로봇

해당 문제는 기능별로 함수를 구현하여 문제를 쉽게 디버깅하고 풀어 나갈 수 있다. 밸트를 자료구조에 남아서 해당 문제를 해결 할 수 있다.

가장 먼저 벨트 위에 올라간 로봇을, 벨트가 회전하는 방향으로 한칸 이동할 수 있다면 이동한다. 이 방법에서 시간을 많이 빼았겼는데 1번 위치는 로봇을 올리는 곳이고, N번의 위치는 로봇을 내리는 위치이기때문에 로봇은 N-1의 위치에 있는곳에 로봇이 있다면 이 로봇이 가장 먼저 컨베이어 벨트에 올라간 로봇이다.

from collections import deque

N, K = map(int, input().split())
zerocount = 0
def rotate(table, robotvisited):
    num = table.pop()
    table.appendleft(num)
    visited = robotvisited.pop()
    robotvisited.appendleft(visited)
    
def moverobot(table, robotvisited):
    global zerocount
    for i in range(N-1, -1, -1):
        if table[i+1] > 0 and not robotvisited[i+1] and robotvisited[i]:
            robotvisited[i] = False
            table[i+1] -= 1
            robotvisited[i + 1] = True
            if table[i+1] == 0:
                zerocount += 1
                
def pushRobot(table, robotvisited):
    global zerocount
    if table[0] > 0: # 내구성이 존재한다면
        robotvisited[0] = True
        table[0] -= 1
        if table[0] == 0:
            zerocount += 1

def popRobot(robotvisited):
    if robotvisited[N - 1]:
        robotvisited[N - 1] = False
    
def vailate(table):
    global zerocount
    if zerocount >= K:
        return False
    else:
        return True

table = deque(map(int, input().split())) # 컨베이어벨트
robotvisited = deque(False for _ in range(len(table))) # 로봇이 있는지 없는지 확인

answer = 0
while vailate(table):
    
    answer += 1
    rotate(table, robotvisited)
    popRobot(robotvisited)
    moverobot(table, robotvisited)
    popRobot(robotvisited)
    pushRobot(table, robotvisited)
    
print(answer)
728x90