본문 바로가기
Algorithm/삼성 SW 역량 테스트 기출 문제

[Baekjoon][20055] 컨베이어 벨트 위의 로봇

by 어발 2022. 6. 27.

출처 - 백준사이트

20055. 컨베이어 벨트 위의 로봇

나의 풀이

해당 문제는 어렵지 않으니, 주석 참고

#include <iostream>
#include <deque>

using namespace std;

int N, K, ret;
deque<pair<int, deque<bool>>> map;

bool check_zero() {
    int cnt = 0;

    for (int idx = 0 ; idx < 2*N ; idx++) {
        if (map[idx].first == 0)
            cnt++;
    }

    return cnt >= K ? true : false;
}

int main () {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    cin >> N >> K;

    for (int idx = 0 ; idx < 2*N ; idx++) {
        int input;
        cin >> input;
        map.push_back(make_pair(input, deque<bool>()));
    }

    while(!check_zero()) {
        ret++;

        // step 1. 벨트회전
        pair<int,deque<bool>> temp = map.back();
        map.pop_back();
        map.push_front(temp);

        // step 1-1. 로봇 내리기
        if (map[N-1].second.size() > 0)
            map[N-1].second.clear();

        // step 2. 로봇 이동
        /* step 2-1. N-1칸에서 N칸인 경우만 따로 고려하기, 이유는 N칸에 로봇이 도착하면 바로 내리게 된다면, N-1칸에 있는 나중에 들어온 로봇도 또 N칸으로 이동이 가능하니까.
                    N칸에 올리자마자 내릴거니까, 올리는 과정은 생략하고 내구도만 감소시킨다. */    
        while ((map[N-2].second.size() > 0) && (map[N-1].first > 0)) {
            map[N-2].second.pop_front();
            map[N-1].first--;
        }

        // step 2-2. N-1칸이 아닌 일반적인 경우.
        for (int idx = N-2 ; idx > 0 ; idx--) {
            if (map[idx-1].second.size() > 0 && map[idx].second.empty() && map[idx].first > 0) { // 로봇이 이동하는데 앞칸이 비어있고, 내구도가 1이상인경우   
                map[idx-1].second.pop_front();
                map[idx].second.push_back(true);
                map[idx].first--;
            }
        }

        // step 3. 로봇 올리기
        if (map[0].first != 0) {
            map[0].second.push_back(true);
            map[0].first--;
        }
    }

    cout << ret;

    return 0;
}
728x90

댓글