[SWEA][Python]#4012. 요리사

MEIN_FIGUR·2021년 8월 17일
0

SWEA_문제풀이

목록 보기
16/21

📌풀이


내가 쓴 풀이(성공)

  • 주어진 재료를 조합하여 나오는 음식들의 합을 위한 함수 food-making
  • 조합들을 구하기 위해 combinations 활용
  • set를 활용해 양 쪽 인덱스 값들을 분리
from itertools import combinations as com

#주어진 재료들을 조합하여 나오는 음식들의 합
def food_making(arr, case):
    res = 0
    for f1, f2 in com(case,2):
        res += arr[f1][f2] + arr[f2][f1]
    return res


T = int(input())
for tc in range(1, T + 1):
    n = int(input())
    arr = []

    for _ in range(n):
        arr.append(list(map(int, input().split())))

    res = 40000
    for case in com(range(n), n//2):
        another = list(set(range(n)) - set(case))
        res1 = food_making(arr, case)
        res2 = food_making(arr, another)
        if res > abs(res1-res2):
            res = abs(res1-res2)

    print(f'#{tc} {res}')



📌후기


처음에 무조건 2개씩 요리되는 경우들 사이에서 값을 비교하는 줄 알고, 테스트 케이스가 답이 틀려 고민이 많았다. 문제의 Q&A에서 두 종류로 재료를 나누고, 거기서의 조합들의 합을 구한다는 점을 깨닫고 다시 진행하였다. 경우의 수를 분리하는 방법을 combinations를 활용하여 하였는데, 다른 방법이 있는지 찾아봐야겠다!

profile
Growing Developer

0개의 댓글