본문 바로가기
알고리즘/백준

백준 20055 컨베이어 벨트 위의 로봇 C++

by ash9river 2024. 8. 18.

간단한 해설

지문이 거지같아서 문제가 어렵다고 느낀 문제

 

1번 위치에서 로봇을 올리고, N번 위치에 로봇이 도착하면 로봇을 무조건 내린다.(내릴 때, 내구도는 소모하지 않음)

 

 

이하의 스크린샷에 담긴 모든 과정의 사이클이 돌아야 한 단계이다.(서식오류로 12134로 된 듯하다.)

 

 

다음의 링크를 통해 개선된 지문을 확인할 수 있다.

 

problem-solve-hub/백준/Gold/20055. 컨베이어 벨트 위의 로봇/이해를 위한 문제 수정본.md at main · c

This is a auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - codesver/problem-solve-hub

github.com

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v;
vector<bool> doesRobotExist;
int ans=1;
int n,k;
bool isEnd(){
    int gae=0;
    for(int i=0;i<2*n;++i){
        if(v[i]==0) ++gae;
    }
    if(gae>=k) return true;
    return false;
}
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    cin>>n>>k;
    v.resize(2*n,0);
    for(int i=0;i<2*n;++i){
        cin>>v[i];
    }
    doesRobotExist.resize(2*n,false);
    while(true){
        // rotate
        vector<int> tmp(2*n,0);
        vector<bool> visitedTmp(2*n,false);
        for(int i=1;i<2*n;++i){
            tmp[i]=v[i-1];
            visitedTmp[i]=doesRobotExist[i-1];
        }
        tmp[0]=v[2*n-1];
        v=tmp;
        doesRobotExist=visitedTmp;
        doesRobotExist[n-1]=false;
        // move
        for(int i=n-2;i>=0;--i){
            if(v[i+1]>0&&!doesRobotExist[i+1]&&doesRobotExist[i]){
                --v[i+1];
                doesRobotExist[i+1]=true;
                doesRobotExist[i]=false;
            }
        }
        doesRobotExist[n-1]=false;
        // lay down
        if(v[0]>0){
            --v[0];
            doesRobotExist[0]=true;
        }
        if(isEnd()) break;
        ++ans;
    }
    cout<<ans;
}

'알고리즘 > 백준' 카테고리의 다른 글

백준 1939 중량제한 C++  (0) 2024.08.19
백준 14222 배열과 연산 C++  (0) 2024.08.07
백준 1781 컵라면 C++  (2) 2024.08.03
백준 30023 전구 상태 바꾸기 C++  (2) 2024.06.18
백준 2247 실질적 약수 C++  (0) 2024.06.12