알고리즘

[알고리즘] 백준 20055 - 컨베이어 벨트 위의 로봇 (C++)

blueberrysoda 2025. 1. 2. 13:44

[문제 링크] : https://www.acmicpc.net/problem/20055

#include <iostream>
#include <queue>
using namespace std;

int Arr[201];
bool Check[201];
queue<int> Q;
int Ans, Start, End, T;

int main(){
    int N, K;
    cin >> N >> K;

    Start = 1;
    End = N;

    for(int i=1; i<=N*2; i++){
        cin >> Arr[i];
    }

    while(Ans < K){
        T++;
        Start = Start == 1 ? N * 2 : Start - 1;
        End = End == 1 ? N * 2 : End - 1;

        int sz = Q.size();
        for(int i=0; i<sz; i++){
            int cur = Q.front();
            Q.pop();

            if(cur == End){
                Check[cur] = false;
                continue;
            }

            int next = cur == 2 * N ? 1 : cur + 1;
            if(Arr[next] == 0 || Check[next] == true){
                Q.push(cur);
            }
            else if(next == End){
                Check[cur] = false;
                Arr[next]--;
                if(Arr[next] == 0){
                    Ans++;
                }
            }
            else{
                Q.push(next);
                Check[cur] = false;
                Check[next] = true;
                Arr[next]--;
                if(Arr[next] == 0){
                    Ans++;
                }
            }     
        }

        if(Arr[Start] > 0){
            Q.push(Start);
            Check[Start] = true;
            Arr[Start]--;
            if(Arr[Start] == 0){
                Ans++;
            }
        }
    }

    cout << T << "\n";
    return 0;
}