1seul357

[SWEA] 정사각형 방 본문

알고리즘/SWEA

[SWEA] 정사각형 방

1seul 2021. 11. 28. 16:09
def dfs(row, col, cnt, tmp):
    dr = [-1, 1, 0, 0]
    dc = [0, 0, -1, 1]
    global max_cnt, min_num
    if cnt > max_cnt:
        max_cnt = cnt
        min_num = tmp
    if cnt == max_cnt:
        if min_num > tmp:
            min_num = tmp
    for i in range(4):
        now_dr = row + dr[i]
        now_dc = col + dc[i]
        if 0 <= now_dr < N and 0 <= now_dc < N:
            if MAP[now_dr][now_dc] - MAP[row][col] == 1:
                dfs(now_dr, now_dc, cnt+1, tmp)


T = int(input())
for TC in range(T):
    N = int(input())
    MAP = [list(map(int, input().split())) for _ in range(N)]
    max_cnt = 0
    min_num = 99999

    for i in range(N):
        for j in range(N):
            dfs(i, j, 1, MAP[i][j])

    print('#{} {} {}'.format(TC+1, min_num, max_cnt))

'알고리즘 > SWEA' 카테고리의 다른 글

[SWEA] 격자판의 숫자 이어붙이기  (0) 2021.11.28
[SWEA] 장훈이의 높은 선반  (0) 2021.11.28
[SWEA] 그룹 나누기  (0) 2021.11.28
[SWEA] 이진 탐색  (0) 2021.11.28
[SWEA] 최소 이동 거리  (0) 2021.11.28