Today
Total
07-05 02:35
관리 메뉴

T-coding

백준 1029 그림 교환 / Python 본문

Baekjoon

백준 1029 그림 교환 / Python

Tcoding 2023. 3. 18. 18:18
728x90
728x90

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

 

1029번: 그림 교환

첫째 줄에 예술가의 수 N이 주어진다. N은 2보다 크거나 같고, 15보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 N개의 수가 주어진다. i번째 줄의 j번째 수는 j번 예술가가 i번 예술가에

www.acmicpc.net

 

 

코드

더보기
n = int(input())
arr = []
for i in range(n):
    arr.append([int(j) for j in input()])

dp = [[[0] * 10 for _ in range(n)] for _ in range(1 << n)]


def dfs(now, artist, cost):
    if dp[now][artist][cost] != 0:
        return dp[now][artist][cost]

    cnt = 0
    for i in range(1, n):
        if arr[artist][i] >= cost and now & (1 << i) <= 0:
            cnt = max(cnt, 1 + dfs(now | (1 << i), i, arr[artist][i]))
    dp[now][artist][cost] = cnt

    return cnt


print(dfs(1, 0, 0) + 1)
728x90
300x250

'Baekjoon' 카테고리의 다른 글

백준 1913 달팽이 / Python  (0) 2023.03.18
백준 1074 Z / Python  (0) 2023.03.18
백준 1022 소용돌이 예쁘게 출력하기 / Python  (0) 2023.03.18
백준 7569 토마토 / Python  (0) 2023.03.17
백준 25206 너의 평점은 / Python  (0) 2023.03.16