백준 :: 컨베이어 벨트 위의 로봇 <20055번>

혜 콩·2022년 9월 5일
0

알고리즘

목록 보기
58/61

> 문제 <

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

> 풀이 <

컨베이어 벨트의 움직임을 리스트 pop() + insert() 를 통해 맨 뒤의 요소를 빼서 맨 앞으로 보낸다.

> 코드 <

N, K = map(int, input().split())
a = list(map(int, input().split()))         # 각 칸의 내구도 (2N개의 칸)
robot = [False] * (N*2)

def rotate(step):
    while 1:
        step += 1
        final, f_robot = a.pop(), robot.pop()
        a.insert(0, final)
        robot.insert(0, f_robot)

        if robot[N-1] == True:
            robot[N-1] = False

        for i in range(N-2, -1, -1):          # 2번 스텝
            if robot[i] == True and robot[i+1] == False and a[i+1] >= 1:
                robot[i], robot[i+1] = False, True
                a[i+1] -= 1
            if robot[N - 1] == True:
                robot[N - 1] = False

        if a.count(0) >= K:
            break

        if a[0] != 0:           # 3번 스텝
            robot[0] = True
            a[0] -= 1

        if a.count(0) >= K:
            break

    return step

print(rotate(0))
profile
배우고 싶은게 많은 개발자📚

0개의 댓글