최소 배열 합 문제와 완전히 동일한 백트래킹 문제입니다.
def dfs(depth, total):
global min_sum
if depth == N:
min_sum = min(min_sum, total)
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, total + matrix[depth][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')
dfs(0, 0)
print(f'#{test} {min_sum}')
'practivceAlgorithm > swexpertacademy' 카테고리의 다른 글
[SWEA][Python] 5248 그룹 나누기 (0) | 2021.10.06 |
---|---|
[SWEA][Python] 5247 연산 (0) | 2021.10.06 |
[SWEA][Python] 5208 전기버스2 (0) | 2021.10.06 |
[SWEA][Python] 5205 퀵정렬 (0) | 2021.10.02 |
[SWEA][Python] 5204 병합정렬 (0) | 2021.10.02 |