[BOJ]コンベアのロボット-20055


📃 質問する
[BOJ]コンベア上のロボット🔗リンク
アクセス
  • 題は4つの動作に分かれています.
    ①ベルト、ロボットが1コマ回転します.
    ②最初にベルトをつけたロボットから、前に一コマ移動できるようにしましょう.
        (ロボットがあるか、耐久性があるかをチェック)
    ③上の位置に格子の耐久性がある場合は、ロボットを上に置きます.
    ④耐久度が0のマス数がK個を超えると終了する.
  • 🧠 に答える
    import sys
    from collections import deque
    input = sys.stdin.readline
    
    N, K = map(int, input().split())
    conveyor = deque(list(map(int, input().split())))
    boxRobot = deque([0]*N)
    count = 0
    
    while(True):
        count += 1
    
        ## Step 1 : Move Conveyor
        conveyor.rotate(1)
        boxRobot.rotate(1)
        boxRobot[-1] = 0
    
        ## Step 2 : Move Robots
        if sum(boxRobot):
            for idx in range(N-2, -1, -1):
                if(boxRobot[idx] and boxRobot[idx+1] == 0 and conveyor[idx+1]):
                    boxRobot[idx] = 0
                    boxRobot[idx+1] = 1
                    conveyor[idx+1] -= 1
        boxRobot[-1] = 0
    
        ## Step 3 : Put Robot on the first
        if(conveyor[0]):
            boxRobot[0] = 1
            conveyor[0] -= 1
            
        ## Step 4 : Counting Durability
        if(conveyor.count(0) >= K):
            break
    
    print(count)