마찬가지로 백트래킹 문제.
def dfs(depth, cur_node, total):
global min_sum
if depth == N-1:
min_sum = min(min_sum, total + matrix[cur_node][0])
return
if total > min_sum:
return
for next_node in range(N):
if not col_check[next_node]:
col_check[next_node] = True
dfs(depth + 1, next_node, total + matrix[cur_node][next_node])
col_check[next_node] = False
for test in range(1, int(input()) + 1):
N = int(input())
matrix = [list(map(int, input().split())) for _ in range(N)]
col_check = [False] * N
min_sum = float('inf')
col_check[0] = True
dfs(0, 0, 0)
print(f'#{test} {min_sum}')
'practivceAlgorithm > swexpertacademy' 카테고리의 다른 글
[SWEA][Python] 5202 화물도크 (0) | 2021.09.28 |
---|---|
[SWEA][Python] 5201 컨테이너 운반 (0) | 2021.09.28 |
[SWEA][Python] 5188 최소합 (0) | 2021.09.28 |
[SWEA][Python] 10966 물놀이를 가자 (1) | 2021.09.24 |
[SWEA][Python] 1953 탈주범 검거 (0) | 2021.09.24 |