Algorithem/백준 PS with code

백준 #20055 - [G5] 컨베이어 벨트 위의 로봇 : 구현

jamong5 2023. 5. 31. 13:37

(python3)

 

20055번: 컨베이어 벨트 위의 로봇

길이가 N인 컨베이어 벨트가 있고, 길이가 2N인 벨트가 이 컨베이어 벨트를 위아래로 감싸며 돌고 있다. 벨트는 길이 1 간격으로 2N개의 칸으로 나뉘어져 있으며, 각 칸에는 아래 그림과 같이 1부

www.acmicpc.net

 

deque 고리 구조

처음과 끝이 연결된 구조이기 때문에 deque 자료구조를 사용해서 pop right, append left 로 맨앞과 맨뒤를 연결해주었습니다.

그 외에는 문제에 주어진 조건대로 처리해주면 됩니다. 내리는 위치에 도달할 때 마다 로봇을 내려주는걸 잘 챙겨주면 될 것 같습니다.

 

코드

from collections import deque
def solution(input) : 
    # 입력 받기
    N,K = list(map(int, input().split()))
    S_BELT = list(map(int,input().split()))

    # deque 자료구조로 벨트 저장 (고리 구조)
    belt = deque()
    for i in S_BELT :
        belt.append([i,False])
    zeros = [0]

    def round1() :
        poped = belt.pop()
        belt.appendleft(poped)
        belt[N-1][1] = False # 내리는 위치 도달 시 내리기

    def round2() :
        for i in range(N-2,-1,-1) : # 뒤에서부터 처리
            if belt[i][1] == True and belt[i+1][1] == False and belt[i+1][0] >= 1 :
                belt[i+1][1] = True
                belt[i+1][0] -= 1
                if belt[i+1][0] == 0 :
                    zeros[0] += 1
                belt[i][1] = False
            belt[N-1][1] = False # 내리는 위치 도달 시 내리기
    
    def round3() :
        if belt[0][0] >= 1 :
            belt[0][0] -= 1
            if belt[0][0] == 0 :
                zeros[0] += 1
            belt[0][1] = True

    def round4() :
        if zeros[0] >= K :
            return True
    
    cnt = 0 # 단계
    while True :
        cnt+=1 
        round1()
        round2()
        round3()
        if round4() :
            break
    print(cnt)

solution(input)

 

my solved.ac :

 

solved.ac

알고리즘 문제해결 학습의 이정표 🚩 Baekjoon Online Judge 문제들의 난이도 및 티어 정보를 제공하는 사이트입니다.

solved.ac