304. 최대상금

아현·2021년 9월 17일
0

Algorithm

목록 보기
321/400



1. DFS


def dfs(count):
    global answer
    if not count:
        temp = int(''.join(values))
        if answer < temp:
            answer = temp
        return

    for i in range(length):
        for j in range(i + 1, length):
            values[i], values[j] = values[j], values[i]
            k = ''.join(values)
            if (k, count - 1) not in visited:
                visited.append((k, count - 1))
                dfs(count - 1)
            values[i], values[j] = values[j], values[i]
 
 
for t in range(1, int(input()) + 1):
    answer = -1
    value, change = input().split()
    values = list(value)
    change = int(change)
    length = len(values)
    visited = []
    dfs(change)
    #print(visited)
    print(f'#{t} {answer}')
profile
Studying Computer Science

0개의 댓글