Notice
Recent Posts
Recent Comments
05-10 16:01
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

-

[백준 2116] 주사위쌓기 - 파이썬 본문

Algorithm

[백준 2116] 주사위쌓기 - 파이썬

choiht 2024. 2. 28. 19:07
반응형

문제

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

 

2116번: 주사위 쌓기

첫줄에는 주사위의 개수가 입력된다. 그 다음 줄부터는 한 줄에 하나씩 주사위의 종류가 1번 주사위부터 주사위 번호 순서대로 입력된다. 주사위의 종류는 각 면에 적혀진 숫자가 그림1에 있는

www.acmicpc.net

 

 

 

풀이 방법

 

주사위의 개수와 주사위 정보를 입력받는다. 

가장 밑에 오는 주사위에서 밑면을 무엇으로 해야 최대가 되는지 계산하기 위해 for 문을 1 - 6까지 돈다. 

밑에 있는 주사위의 윗면과, 위에 있는 주사위의 밑면이 일치해야하기 때문에 그 수를 맞춘 상태에서 옆면이 최대가 되는 숫자를 더해준다.

for 문이 끝날 때마다 최대가 되는 옆면의 합을 갱신한다.

 

 

 

코드

n = int(input())
dice_list = [list(map(int, input().split())) for _ in range(n)]
max_sum = 0

def find_max(dice, bottom):
    for i in range(6):
        if dice[i] == bottom:
            break
    
    if i == 0:
        return (dice[5], max(dice[1], dice[2], dice[3], dice[4]))
    elif i == 1:
        return (dice[3], max(dice[0], dice[2], dice[4], dice[5]))
    elif i == 2:
        return (dice[4], max(dice[0], dice[1], dice[3], dice[5]))
    elif i == 3:
        return (dice[1], max(dice[0], dice[2], dice[4], dice[5]))
    elif i == 4:
        return (dice[2], max(dice[0], dice[1], dice[3], dice[5]))
    elif i == 5:
        return (dice[0], max(dice[1], dice[2], dice[3], dice[4]))



for i in range(1, 7):
    bottom = i
    total = 0
    
    for j in range(n):
        bottom, maxnum = find_max(dice_list[j], bottom)
        total += maxnum
        
    if max_sum < total:
        max_sum = total
        
print(max_sum)
반응형
Comments