BOJ - 14889 - 스타트와 링크

Updated:

import sys

minValue = 1e9

def DFS(idx, trueCount, dataArray, checkArray):
    global minValue
    if idx == len(dataArray):
        return

    if trueCount == len(dataArray) // 2:
        start = 0
        link = 0
        for i in range(len(dataArray)):
            for j in range(len(dataArray)):
                if checkArray[i] and checkArray[j]:
                    start += dataArray[i][j]
                if not checkArray[i] and not checkArray[j]:
                    link += dataArray[i][j]

        minValue = min(abs(start - link), minValue)
        return

    checkArray[idx] = True
    DFS(idx + 1, trueCount + 1, dataArray, checkArray)
    checkArray[idx] = False
    DFS(idx + 1, trueCount, dataArray, checkArray)


def solution():
    dataArray = []

    N = int(sys.stdin.readline())
    for _ in range(N):
        dataArray.append(list(map(int, sys.stdin.readline().split(' '))))

    checkArray = [False for _ in range(N)]
    idx = 0
    trueCount = 0

    checkArray[idx] = True
    DFS(idx + 1, trueCount + 1, dataArray, checkArray)
    checkArray[idx] = False
    DFS(idx + 1, trueCount, dataArray, checkArray)
    print(minValue)


solution()

https://www.acmicpc.net/problem/14889

Categories:

Updated:

Leave a comment