https://www.codetree.ai/missions/2/problems/max-sum-of-numbers/description
n = int(input())
graph = []
for _ in range(n):
temp = list(map(int, input().rstrip().split(" ")))
graph.append(temp)
visited = [0 for _ in range(n)]
row = [i for i in range(n)]
num = [i for i in range(n)]
cnt = 0
result = []
answer = []
def find_max(col):
cnt = 0
for i, j in zip(row, col):
cnt += graph[i][j]
return cnt
def backtrack(dep):
global cnt
if dep == n:
temp = answer[::]
result.append(temp)
cnt = max(find_max(temp), cnt)
return
for i in range(n):
if not visited[i]:
answer.append(num[i])
visited[i] = 1
backtrack(dep+1)
answer.pop()
visited[i] = 0
backtrack(0)
print(cnt)
728x90
반응형
'🔻PS > Codetree' 카테고리의 다른 글
[Codetree] 수들 중 최솟값 최대화하기 Python (0) | 2024.09.12 |
---|---|
[Codetree] 외판원 순회 Python (0) | 2024.09.12 |
[Codetree] 1차원 윷놀이 Python (3) | 2024.09.08 |
[Codetree] 강력한 폭발 Python (0) | 2024.09.05 |
[Codetree] 아름다운 수 Python (1) | 2024.09.05 |