[BOJ][๐ŸŸก4][๋ฐฑ์ค€#14925] ๋ชฉ์žฅ ๊ฑด์„คํ•˜๊ธฐ

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #14925


๋ฌธ์ œ

๋žœ๋“œ ์”จ๋Š” ํ‡ด์ง๊ธˆ์œผ๋กœ ๋•…์„ ์‚ฌ์„œ ๋ชฉ์žฅ์„ ์ง€์œผ๋ ค ํ•œ๋‹ค.ย  ๊ทธ๊ฐ€ ์‚ฌ๋ ค๊ณ  ์†Œ๊ฐœ๋ฐ›์€ ๋•…์€ ์ง์‚ฌ๊ฐํ˜•์ด๊ณ  ๋Œ€๋ถ€๋ถ„ ๋“คํŒ์ด์ง€๋งŒ, ์—ฌ๊ธฐ์ €๊ธฐ์— ๋ฒ ๊ธฐ ์–ด๋ ค์šด ๋‚˜๋ฌด์™€ ์น˜์šธ ์ˆ˜ ์—†๋Š” ๋ฐ”์œ„๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋Š” ๋ชฉ์žฅ์„ ํ•˜๋‚˜์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ์ตœ๋Œ€ํ•œ ํฌ๊ฒŒ ์ง€์œผ๋ ค ํ•˜๋Š”๋ฐ, ๊ทธ ์•ˆ์— ๋‚˜๋ฌด๋‚˜ ๋ฐ”์œ„๋Š” ์—†์–ด์•ผ ํ•œ๋‹ค.ย  ๋•…์˜ ์„ธ๋กœ ๊ธธ์ด๊ฐ€ M๋ฏธํ„ฐ, ๊ฐ€๋กœ ๊ธธ์ด๊ฐ€ N๋ฏธํ„ฐ์ผ ๋•Œ, 1๋ฏธํ„ฐ ๊ฐ„๊ฒฉ์˜ ๊ฒฉ์ž๋กœ ๋œ ๋•…์˜ ์ง€๋„๋ฅผ M x Nํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜์ž.ย  ์ด๋•Œ, ํ–‰๋ ฌ์˜ ์›์†Œ 0์€ ๋“คํŒ, 1์€ ๋‚˜๋ฌด ๊ทธ๋ฆฌ๊ณ  2๋Š” ๋Œ์„ ์˜๋ฏธํ•œ๋‹ค.ย  ๋žœ๋“œ์”จ์˜ ๋•…์—์„œ ์ง€์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜• ๋ชฉ์žฅ์˜ ํ•œ ๋ณ€์˜ ํฌ๊ธฐ L์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.


์ž…๋ ฅ

M N M x N ํ–‰๋ ฌ

1 <= M <= 1000 1 <= N <= 1000


์ถœ๋ ฅ

L


์˜ˆ์ œ

์ž…๋ ฅ

6 6
0 0 0 1 0 0
0 0 0 2 1 0
0 0 2 0 0 0
0 1 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0


์ถœ๋ ฅ

3


My Sol

I, J = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(I)]
mat_h = [[0]*(J+1) for _ in range(I+1)]
mat_v = [[0]*(J+1) for _ in range(I+1)]

for i in range(1, I+1):
    for j in range(1, J+1):
        if mat[i-1][j-1]: continue
        mat_h[i][j] = mat_h[i][j-1] + 1

for j in range(1, J+1):
    for i in range(1, I+1):
        if mat[i-1][j-1]: continue
        mat_v[i][j] = mat_v[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 mat[i-1][j-1]: continue
        dp[i][j] = min(mat_v[i][j], mat_h[i][j], dp[i-1][j-1]+1)

maxx = 0
for i in range(1, I+1):
    for j in range(1, J+1):
        if maxx < dp[i][j]:
            maxx = dp[i][j]
print(maxx)
  1. ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.
  2. mat 2์ฐจ์› ๋ฐฐ์—ด์˜ ๊ฐ€๋กœ ์„ธ๋กœ์˜ ์—ฐ์†๋œ 0์˜ ๊ธธ์ด๋ฅผ ๊ฐ๊ฐ mat_h์™€ mat_v์— ํ‘œ์‹œํ•ด์ค€๋‹ค.
  3. dp ๋ฐฐ์—ด์— ํ˜„์žฌ ์œ„์น˜์˜ ๊ฐ€๋กœ ์„ธ๋กœ ์—ฐ์† ๊ธธ์ด, ๊ทธ๋ฆฌ๊ณ  ์™ผ์ชฝ ์œ„ ์ตœ๋Œ€๊ธธ์ด+1 ๊ฐ’ ์ค‘ ์ตœ์†Ÿ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ์ด๋Š” ๊ณง ํ˜„์žฌ ์œ„์น˜๋ฅผ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ์ ์œผ๋กœ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜•์˜ ์ตœ๋Œ€ ๋ณ€ ๊ธธ์ด์ด๋‹ค.
  4. ๋ชจ๋“  dp ๋ฐฐ์—ด์— ๋Œ€ํ•ด ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

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


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

์ถœ์ฒ˜

import sys
input = sys.stdin.readline

n, m = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(n)]
dp = [[0]*(m+1) for _ in range(n+1)]

for i in range(1,n+1):
    for j in range(1,m+1):
        if board[i-1][j-1]:
            dp[i][j] = 0
        else:
            dp[i][j] = min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1
            
print(max(map(max, dp)))

ํ˜„์žฌ ์œ„์น˜์— 0์ด ์•„๋‹ˆ๋ผ๋ฉด dp์— 0์„ ๋‘๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์œ„, ์™ผ์ชฝ, ์™ผ์ชฝ ์œ„ 3๋ฐฉํ–ฅ์˜ dp ๊ฐ’ ์ค‘ ์ตœ์†Ÿ๊ฐ’ +1์„ ํ˜„์žฌ dp์— ์ €์žฅํ•œ๋‹ค.

์—ฐ์†๋œ 0์„ ๊ณ„์‚ฐํ•˜๋Š” mat_v, mat_h์˜ ๊ณผ์ •์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ›จ์”ฌ ๋น ๋ฅธ ์—ฐ์‚ฐ์†๋„๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฏธ dp ๋ฐฐ์—ด์— 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋˜์—ˆ์œผ๋ฏ€๋กœ board[i-1][j-1] ์ด 0์ด ์•„๋‹ˆ๋ผ๋ฉด dp์— 0์„ ์žฌํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ continue ํ•˜๋Š” ๊ฒƒ์ด ๋ฉ”๋ชจ๋ฆฌ ์ธก๋ฉด์ด๋‚˜ ์‹œ๊ฐ„ ์ธก๋ฉด์—์„œ๋„ ๋” ์œ ๋ฆฌํ•  ๋“ฏ ์‹ถ๋‹ค.

2์ฐจ์› ๋ฐฐ์—ด ๋‚ด์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์„ print(max(map(max, dp)))๋กœ ๊ต‰์žฅํžˆ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋‹ค.

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