ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 1915 ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜• with Python
    PS 2022. 6. 17. 11:50
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 1915 ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ˆ์ œ์—์„œ๋Š” ๊ฐ€์šด๋ฐ์˜ 2×2 ๋ฐฐ์—ด์ด ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์ด๋‹ค.

    1. ์ฒซ์งธ ์ค„์— n, m(1 ≤ n, m ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค.
      ๋‹ค์Œ n๊ฐœ์˜ ์ค„์—๋Š” m๊ฐœ์˜ ์ˆซ์ž๋กœ ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค.
    1. n×m์˜ 0, 1๋กœ ๋œ ๋ฐฐ์—ด์ด ์žˆ๋‹ค. ์ด ๋ฐฐ์—ด์—์„œ 1๋กœ ๋œ ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.
    1. DP, ๋‹ค์ด๋‚˜๋ฏนํ”„๋กœ๊ทธ๋ž˜๋ฐ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    """
    
    """
    n, m = map(int, stdin.readline().split())
    arr = []
    for _ in range(n):
        arr.append(list(map(int, list(stdin.readline().rstrip()))))
    
    ans = 0
    dp = [[0] * (m + 1) for _ in range(n + 1)]
    
    for i in range(1, n + 1):
        for j in range(1, m + 1):
            if arr[i - 1][j - 1] == 1:
                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
                ans = max(ans, dp[i][j])
    
    print(ans ** 2)

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    4 4
    0100
    0111
    1110
    0010

    ์‹คํ–‰๊ฒฐ๊ณผ

    4

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•ด์•ผํ•˜๋Š”๋ฐ, ์ •์‚ฌ๊ฐํ˜•์ด ๋˜๋Š” ์ƒํƒœ๋ฅผ dp ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋ฉด์„œ ๊ฐ€์žฅ ํฐ ๋ณ€์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.
    1. ํ•œ ์นธ๋„ ์ •์‚ฌ๊ฐํ˜•์ด ๋  ์ˆ˜ ์žˆ๊ธฐ์—, ์ตœ์†Œ ๋„“์ด์˜ ๊ฐ’์€ 1์ด๋‹ค.
    1. ํ˜„์žฌ ์ˆœํšŒํ•˜๊ณ  ์žˆ๋Š” ์ขŒํ‘œ x, y ๋ฅผ ๊ธฐ์ค€์œผ๋กœ x-1, y-1 ์ด 1์ผ ๋•Œ
      dp ๋ฆฌ์ŠคํŠธ์˜ ํ˜„์žฌ ์ˆœํšŒํ•˜๋Š” ์ขŒํ‘œ x, y๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ, ์œ„, ์™ผ์ชฝ ๋Œ€๊ฐ์„ ์„ ์‚ดํŽด๋ณด๊ณ  ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ + 1์„ ๊ตฌํ•ด
      dp[x][y]์— ์ €์žฅํ•˜๊ณ , ์ด ๊ฐ’์„ ans ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ฐ€์žฅ ํฐ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.
    1. ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ชจ๋‘ ์ˆœํšŒํ–ˆ๋‹ค๋ฉด, ์ €์žฅ๋œ ans ๊ฐ’์„ ์ œ๊ณฑํ•˜์—ฌ ๋„“์ด๋ฅผ ๊ตฌํ•˜๊ณ  ์ถœ๋ ฅํ•œ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.