BOJ - 1937 - 욕심쟁이 판다

Updated:

import sys
sys.setrecursionlimit(10**9)

dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

def DFS(maps, dp, x, y, N):
    if dp[x][y]:
        return dp[x][y]
    dp[x][y] = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx < N and 0 <= ny < N:
            if maps[x][y] < maps[nx][ny]:
                dp[x][y] = max(dp[x][y], DFS(maps, dp, nx, ny, N) + 1)
    return dp[x][y]
def solution():
    N = int(input())
    maps = []
    dp = [[0] * N for _ in range(N)]
    for _ in range(N):
        maps.append(list(map(int, sys.stdin.readline().split())))

    result = 0
    for i in range(N):
        for j in range(N):
            result = max(result, DFS(maps, dp, i, j, N))

    print(result)
solution()

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

Categories:

Updated:

Leave a comment