백준 13335번 c++ : 트럭

Rena·2023년 1월 5일
0

알고리즘 문제풀이

목록 보기
37/45
#include <bits/stdc++.h>
using namespace std;

int n, w, L, ans;
int bridge[100]; //다리 위치별 무게
queue<int> truck;

bool isEmpty() {
    for(int i=0; i<w; i++) {
        if(bridge[i]) return false;
    }
    return true;
}

int calculate() {
    int sum = 0;
    for(int i=0; i<w; i++) {
        sum += bridge[i];
    }
    return sum;
}

void go() {
    for(int i=w-1; i>0; i--) { //다리위 트럭 한 칸씩 이동
        bridge[i] = bridge[i-1];
    }
    bridge[0] = 0;
}

int main(void){ 
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> w >> L ;
    for(int i=0; i<n; i++) {
        int t;
        cin >> t;
        truck.push(t);
    }

    do{
        int tmp = calculate(); 
        tmp -= bridge[w-1]; //나갈 트럭 무게 빼기
        go();
        if(!truck.empty() && tmp + truck.front() <= L) { //새로 들어올 트럭있는지, 들어와도 최대하중 안넘는지 확인 후
            bridge[0] = truck.front(); truck.pop(); //새로운 트럭 진입
        }
        ans++;
    }while(!isEmpty());

    cout << ans;
}
profile
일을 사랑하고 싶은 개발자

0개의 댓글