232. 스타트와 링크

아현·2021년 8월 3일
0

Algorithm

목록 보기
242/400
post-thumbnail

백준




1. DFS


import sys
input = sys.stdin.readline

n = int(input())
board = [list(map(int, input().split())) for _ in range(n)]
visit = [0] * n
result = sys.maxsize


def dfs(index, count):
    global result
    if count == n // 2:
        start, link = 0, 0
        for i in range(n):
            for j in range(n):
                if visit[i] and visit[j] and board[i][j] != 0:
                    start += board[i][j]

                elif not visit[i] and not visit[j] and board[i][j] != 0:
                    link += board[i][j]

        result = min(result, abs(start - link))

    for i in range(index, n):
        if visit[i]:
            continue
        visit[i] = 1
        dfs(i + 1, count + 1)
        visit[i] = 0


dfs(0, 0)
print(result)



profile
For the sake of someone who studies computer science

0개의 댓글