[BOJ][๐ŸŸก4][๋ฐฑ์ค€#01915] ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1915


๋ฌธ์ œ

nร—m์˜ 0, 1๋กœ ๋œ ๋ฐฐ์—ด์ด ์žˆ๋‹ค. ์ด ๋ฐฐ์—ด์—์„œ 1๋กœ ๋œ ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

0 1 0 0

0 1 1 1

1 1 1 0

0 0 1 0

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


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— n, m(1 โ‰ค n, m โ‰ค 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ n๊ฐœ์˜ ์ค„์—๋Š” m๊ฐœ์˜ ์ˆซ์ž๋กœ ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

4 4
0100
0111
1110
0010


์ถœ๋ ฅ

4


My Sol

I, J = map(int, input().split())
mat = [list(map(int, input())) for _ in range(I)]

# ๊ฐ€๋กœ ์—ฐ์† 1
h_mat = [[0]*J for _ in range(I)]
for i in range(I):
    h_mat[i][0] = mat[i][0]
for i in range(I):
    for j in range(1, J):
        if not mat[i][j]: continue
        h_mat[i][j] = h_mat[i][j-1]+1

# ์„ธ๋กœ ์—ฐ์† 1
v_mat = [[0]*J for _ in range(I)]
for j in range(J):
    v_mat[0][j] = mat[0][j]
for i in range(1, I):
    for j in range(J):
        if not mat[i][j]: continue
        v_mat[i][j] = v_mat[i-1][j]+1

dp = [[0]*(J+1) for _ in range(I+1)]
for i in range(1, I+1):
    for j in range(1, J+1):
        if not mat[i-1][j-1]: continue
        l = dp[i-1][j-1]
        if not l:
            dp[i][j] = 1
            continue

        if v_mat[i-1][j-1] > l and h_mat[i-1][j-1] > l:
            dp[i][j] = dp[i-1][j-1] + 1
        else:
            dp[i][j] = min(v_mat[i-1][j-1], h_mat[i-1][j-1], l+1)

maxx = 0
for i in range(I+1):
    for j in range(J+1):
        if maxx < dp[i][j]:
            maxx = dp[i][j]
print(maxx**2)

DP๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋ฌธ์ œ ํ’€์ด์˜ ํ•ต์‹ฌ์€ ์–ด๋Š ํฌ์ธํŠธ์—์„œ ์ตœ๋Œ€ ํฌ๊ธฐ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„๋‹ค๋ฉด, ๊ทธ ๋‹ค์Œ ํ–‰๊ณผ ๊ทธ ๋‹ค์Œ ์—ด์ด ๊ณ„์† 1์ด์–ด์•ผ ์˜ค๋ฅธ์ชฝ ๋Œ€๊ฐ์„  ์•„๋ž˜์— 1์ด ๋” ์ฆ๊ฐ€ํ•œ ์ •์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋ฅผ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๊ณ , ๋งŒ์•ฝ ํ–‰๊ณผ ์—ด์—์„œ ์ค‘๊ฐ„์— ์ž˜๋ฆฐ๋‹ค๋ฉด ์ž˜๋ฆฐ ํฌ์ธํŠธ๊นŒ์ง€์˜ ๊ธธ์ด ์ค‘ ๊ฐ€์žฅ ์งง์€ ๊ฒƒ์ด ์ƒˆ๋กœ์šด ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์˜ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

  1. ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  2. ํ–‰๊ณผ ์—ด์—์„œ ์—ฐ์†๋œ 1์„ ์ฒดํฌํ•˜๋Š” h_mat๊ณผ v_mat์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  3. ํ•œ ํฌ์ธํŠธ์˜ ์ตœ๋Œ€ ์ •์‚ฌ๊ฐํ˜•์˜ ๋ณ€์˜ ๊ธธ์ด๋ฅผ ์ €์žฅํ•˜๋Š” dp 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ์ด๋ฅผ ์œ„์˜ ์„ค๋ช…์— ๋งž๊ฒŒ ๋กœ์ง์„ ๊ตฌ์„ฑํ•œ๋‹ค.
  4. dp 2์ฐจ์› ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ํ™•์ธํ•œ๋‹ค. ์ด๋Š” ํ•ด๋‹น ํฌ์ธํŠธ์—์„œ ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์˜ ๋ณ€์˜ ๊ธธ์ด์ด๋ฏ€๋กœ ์ด๋ฅผ ์ œ๊ณฑํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import sys
input = sys.stdin.readline

n, m = map(int,input().split())
arr = []

for i in range(n):
    now = list(map(int, input().rstrip()))
    arr.append(now)

for i in range(1,n):
    for j in range(1,m):
        if arr[i][j] == 1:
            arr[i][j] = min(arr[i-1][j-1], arr[i][j-1], arr[i-1][j]) +1

answer = 0

for i in arr:
    answer = max(answer, max(i))

print(answer**2)

๋ฉ”๋ชจ๋ฆฌ์™€ ์—ฐ์‚ฐ์‹œ๊ฐ„์ด ์ ˆ๋ฐ˜์ •๋„ ๋˜๋Š” ํ’€์ด๋ฅผ ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. ์šฐ์„  stdin.readline์„ ์ด์šฉํ•ด์„œ ์ž…๋ ฅ์„ ๋น ๋ฅด๊ฒŒ ๋ฐ›์•˜๋‹ค. ์ด ๋ฌธ์ œ ๊ฐ™์€ ๊ฒฝ์šฐ ValueError๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ๋‚˜๋Š” ์ œ๊ฑฐํ•ด์ฃผ์—ˆ๋‹ค. DP๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ํ•œ ํฌ์ธํŠธ์˜ ์œ„์ชฝ, ์™ผ์ชฝ, ์™ผ์ชฝ ์œ„ ๋Œ€๊ฐ์„ ์˜ dp ๊ฐ’ ์ค‘ ์ตœ์†Ÿ๊ฐ’์— 1์„ ๋”ํ•œ ๊ฐ’์„ dp์— ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ