๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 20055 - ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ ์œ„์˜ ๋กœ๋ด‡

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 20055๋ฒˆ

ํ’€์ด ๊ณผ์ •

๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ๊ทœ์น™๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š” ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ๋‚˜ํƒ€๋ƒˆ์ง€๋งŒ ์‚ฌ์‹ค ๊ทธ๋ƒฅ 1์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋„ค ๊ฐ€์ง€ ๋‹จ๊ณ„๋ฅผ ๊ฐ๊ฐ์˜ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด์„œ ๊ฒ€์‚ฌํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque

N, K = list(map(int, sys.stdin.readline().split()))
belts = list(map(int, sys.stdin.readline().split()))


def init_conveyor():
    conveyor = [[0] * N for _ in range(2)]
    conveyor[0] = [*belts[:N]]
    conveyor[1] = [*belts[N:][::-1]]

    return conveyor


def rotate_belt(conveyor, robots):
    # ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ ์ด๋™
    cache1 = conveyor[0][N - 1]
    for y in range(N - 1, 0, -1):
        conveyor[0][y] = conveyor[0][y - 1]

    cache2 = conveyor[1][0]
    for y in range(N - 1):
        conveyor[1][y] = conveyor[1][y + 1]

    conveyor[0][0] = cache2
    conveyor[1][N - 1] = cache1

    # ๋กœ๋ด‡ ์ด๋™
    robots.pop()
    robots.appendleft(0)


def move_robots(conveyor, robots):
    for idx in range(N - 1, -1, -1):
        if robots[idx]:
            next_idx = idx + 1
            if robots[next_idx] == 0 and conveyor[0][next_idx] > 0:
                conveyor[0][next_idx] -= 1
                robots[idx] = 0
                robots[next_idx] = 1


def add_robot(conveyor, robots):
    if conveyor[0][0] > 0 and robots[0] != 1:
        robots[0] = 1
        conveyor[0][0] -= 1


def check_belt(conveyor):
    cnt = 0

    for x in range(2):
        for y in range(N):
            if not conveyor[x][y]:
                cnt += 1

    return cnt < K


def check_endpoint(robots):
    if robots[N - 1]:
        robots[N - 1] = 0


def solution():
    answer = 1
    conveyor = init_conveyor()
    robots = deque([0] * N)

    while True:
        rotate_belt(conveyor, robots)
        check_endpoint(robots)
        move_robots(conveyor, robots)
        check_endpoint(robots)
        add_robot(conveyor, robots)

        go = check_belt(conveyor)
        if not go:
            break

        answer += 1

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 9625 - BABBA  (0) 2021.03.08
BOJ 11952 - ์ข€๋น„  (0) 2021.03.08
BOJ 20044 - Project Teams  (0) 2021.03.08
BOJ 1644 - ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ  (0) 2021.03.08
BOJ 18242 - ๋„ค๋ชจ๋„ค๋ชจ ์‹œ๋ ฅ๊ฒ€์‚ฌ  (0) 2021.03.08

๐Ÿ’ฌ ๋Œ“๊ธ€