DevYoon
[SWEA] 4012. ์๋ฆฌ์ฌ (Python) ๋ณธ๋ฌธ
link ๐ https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeUtVakTMDFAVH
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)]์ผ๋ก ํด๋๊ณ ํ์ฐธ ๋๋ฒ๊น ํ๋ค... ํฉ๋น๐