practivceAlgorithm/swexpertacademy

[SWEA][Python] 5209 최소 생산 비용

findTheValue 2021. 10. 6. 06:22

최소 배열 합 문제와 완전히 동일한 백트래킹 문제입니다.

 

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}')