본문 바로가기

CodingTest/SW Expert Academy

[ SW Expert Academy ] 5188. 최소합

헐.. 드디어 재귀를 잘 다룰 수 있게 된건가..? 

재귀 구현 한 큐에 성공한 게 처음이라서 얼떨떨하다.. 

 

1차 시도. [ 10 /10 ]. Pas

def search(x, y, n, board, visited, total):
    total += board[x][y]
    global total_min
    if x == n - 1 and y == n - 1:
        total_min = min(total_min, total)
        return 
    # 오른쪽
    if x + 1 < n:
        search(x+1, y, n, board, visited, total)
    # 아래쪽
    if y + 1 < n:
        search(x, y+1, n, board, visited, total)
    
T = int(input())
for test_case in range(1, T + 1):
    n = int(input())
    total_min = n*n*10
    board = [list(map(int, input().split())) for _ in range(n)]
    visited = [[0]*n for _ in range(n)]
    
    search(0, 0, n, board, visited, 0)
    print("#{} {}".format(test_case, total_min))