๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 14925 - ๋ชฉ์žฅ ๊ฑด์„คํ•˜๊ธฐ

by HandHand 2021. 3. 14.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 14925๋ฒˆ

ํ’€์ด ๊ณผ์ •

๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์„ ์ฐพ๋Š” ๋™์  ๊ณ„ํš๋ฒ• ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ƒฅ ์™„์ „ ํƒ์ƒ‰ ์œผ๋กœ ์ฐพ์œผ๋ ค ํ•  ๊ฒฝ์šฐ O(N^4) ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
dp[x,y] = (x, y)์ง€์ ์„ ์™ผ์ชฝ ๋ชจ์„œ๋ฆฌ๋กœ ํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด ๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค๋ฉด
๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ํ™”์‹์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


dp[x][y] = min(dp[x + 1][y], dp[x][y + 1], dp[x + 1][y + 1]) + 1


์ด๋•Œ ๊ฒฉ์ž๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ์œ„์น˜๋‚˜ ์žฅ์• ๋ฌผ์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ธฐ์ € ์‚ฌ๋ก€๋กœ ์ฒ˜๋ฆฌํ•ด์ค๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys

sys.setrecursionlimit(10**6)

M, N = list(map(int, sys.stdin.readline().split()))
board = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
memo = [[-1] * N for _ in range(M)]


def dp(x, y):
    if x < 0 or x >= M or y < 0 or y >= N:
        return 0
    if board[x][y] != 0:
        return 0

    if memo[x][y] != -1:
        return memo[x][y]

    memo[x][y] = min(dp(x + 1, y), dp(x, y + 1), dp(x + 1, y + 1)) + 1

    return memo[x][y]


def solution():
    answer = 0

    for x in range(M):
        for y in range(N):
            L = dp(x, y)
            answer = max(answer, L)

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 20363 - ๋‹น๊ทผ ํ‚ค์šฐ๊ธฐ  (0) 2021.03.14
BOJ 14588 - Line Friends (Small)  (0) 2021.03.14
BOJ 13164 - ํ–‰๋ณต ์œ ์น˜์›  (0) 2021.03.14
BOJ 3109 - ๋นต์ง‘  (0) 2021.03.14
BOJ 14620 - ๊ฝƒ๊ธธ  (0) 2021.03.14

๐Ÿ’ฌ ๋Œ“๊ธ€