알고리즘 문제 풀이/Python

python 백준 3060 욕심쟁이 돼지

맛대 2023. 9. 5. 17:34

https://www.acmicpc.net/problem/3060

 

3060번: 욕심쟁이 돼지

입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터는 두 줄로 구성되어 있고, 첫째 줄에는 하루에 배달되는 사

www.acmicpc.net

코드 해설

0~5번 돼지 존재

돼지는 전날 좌, 우, 맞은편의 돼지가 먹은 양을 기억 => ls에 기록된 값 이용, 하루가 지나가면 값 갱신(ls = todayFeed)

좌측 돼지(pig -1) :python에서는 list의 index가 -1일 경우 list의 맨 뒤 값을 조회하므로 문제 없음

우측돼지(pig+1) : 가장 마지막 돼지(5번 돼지)의 경우 6번 index를 찾게 되는데, list의 범위를 벗어나므로 나머지 연산으로 0번 조회

맞은편 돼지(pig+3) : 우측 돼지와 마찬가지로 나머지 연산을 이용

오늘 돼지가 먹는 총 양(value) 이 사료 보급량을 넘을 때 까지 반복문

def solution(N:int,ls:list[int])->int:
    value = sum(ls)
    day = 1
    while N>=value:
        day +=1
        todayFeed = [0]*6
        value = 0
        for pig in range(6):
            todayFeed[pig] = ls[pig] + ls[(pig+1)%6] + ls[pig-1] + ls[(pig+3)%6]
            value += todayFeed[pig]
        ls = todayFeed
    return day

T = int(input())
for _ in range(T):
    N = int(input())
    total = list(map(int,input().split()))
    ans = solution(N,total)
    print(ans)