20055 : 컨베이어 벨트

CS·2026년 2월 16일

SSPS

목록 보기
6/10

formula

Grid 밀기

Implementation

#include <iostream>
#include <vector>

using namespace std;

int N, K;

int A[205]; // 내구도 배열
bool robot[205]; // 로봇 존재성


int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> N >> K; // 내리는 위치 , 내구도

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

    //res
    int step = 0;

    while (true) {
        step++;

        // 벨트 회전
        int temp_A = A[2 * N]; // 빼놓고
        for (int i = 2 * N; i > 1; i--) {
            A[i] = A[i - 1];
        }
        A[1] = temp_A; // grid 밀기

        // 로봇 회전
        bool temp_robot = robot[2 * N];
        for (int i = 2 * N; i > 1; i--) {
            robot[i] = robot[i - 1];
        }
        robot[1] = false;

        if (robot[N]) { // N위치 로봇은 즉시 하차
            robot[N] = false;
        }

        // 로봇 이동
        for (int i = N - 1; i >= 1; i--) {
            // 다음칸에 로봇없고 내구도 있는 상황에
            if (robot[i] == true && robot[i + 1] == false && A[i + 1] > 0) {
                // 끝부터 하나씩 순차로 밀어서 이동
                robot[i] = false;
                robot[i + 1] = true;
                A[i + 1]--;

                if (robot[N]) {
                    robot[N] = false; // N은 하차처리
                }
            }
        }

        // 1번 index에 로봇 올리기
        if (A[1] > 0) { // 내구도 있고
            if (robot[1] == false) // 로봇 없으면
            {
                robot[1] = true;
                A[1]--; //올리기
            }
        }

        // 내구도 검사
        int cnt = 0;
        for (int i = 1; i <= 2 * N; i++) {
            if (A[i] == 0) cnt++;
        }
        if (cnt >= K) break;
    }

    cout << step;

}
profile
학습

0개의 댓글