DevYoon

[SWEA] 4012. ์š”๋ฆฌ์‚ฌ (Python) ๋ณธ๋ฌธ

PS/SWEA

[SWEA] 4012. ์š”๋ฆฌ์‚ฌ (Python)

gimewn 2022. 4. 7. 17:33

link ๐Ÿ”— https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeUtVakTMDFAVH 

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

 

def cal(lst):
    temp = 0
    for i in range(len(lst)-1):
        for j in range(i+1, len(lst)):
            temp += arr[lst[i]][lst[j]]
            temp += arr[lst[j]][lst[i]]
    return temp

def dfs(level, num):
    global Min
    if level == (N//2)-1:
        A = []
        B = []
        for idx in range(N):
            if check[idx] == 1:
                A.append(idx)
            else:
                B.append(idx)
        Min = min(Min, abs(cal(A)-cal(B)))
        return
    check[num] = 1
    for number in range(num+1, N):
        if check[number] != 1:
            check[number] = 1
            dfs(level+1, number)
            check[number] = 0

t = int(input())
for tc in range(1, t+1):
    N = int(input())
    arr = [list(map(int, input().split())) for _ in range(N)]
    check = [0 for _ in range(N)]
    Min = 2e18
    dfs(0, 0) # level, num
    print(f'#{tc} {Min}')

 

๐Ÿ’ฅ ์ฃผ์˜

  • ์ฒดํฌ ๋ฐฐ์—ด์„ [0 for _ in range(N)]์œผ๋กœ ๋งŒ๋“ค์—ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ [x for x in range(N)]์œผ๋กœ ํ•ด๋†“๊ณ  ํ•œ์ฐธ ๋””๋ฒ„๊น… ํ–ˆ๋‹ค... ํ™ฉ๋‹น๐Ÿ™ƒ