[ 2023-07-25 ๐Ÿฅผ TIL ]

Burkeyยท2023๋…„ 7์›” 25์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
129/157

๋ฐฑ์ค€ 2468๋ฒˆ ํŒŒ์ด์ฌ


๋ฌธ์ œ



์ฝ”๋“œ

import sys
import copy
from collections import deque

input = sys.stdin.readline
land = []
high = 0
n = int(input())

visited_copy = [[False] * n for _ in range(n)]

for _ in range(n):
  l = list(map(int, input().split()))
  high = max(max(l), high) # ์ž…๋ ฅ๋œ ์„ฌ์˜ ๋†’์ด ์ค‘์— ๊ฐ€์žฅ ๋†’์€ ์„ฌ์„ ์ฐพ์Œ 
  land.append(l)
  
move = ((-1, 0), (1, 0), (0, -1), (0, 1)) # ์ƒํ•˜์ขŒ์šฐ ์ด๋™ 
cnt = 0
result = []  
  
for num in range(high+1):
  visited = copy.deepcopy(visited_copy) # ๊นŠ์€ ๋ณต์‚ฌ
  cnt = 0
  for i in range(n):
    for j in range(n):
      if (not visited[i][j] and land[i][j] > num):
      # (land[i][j] > num) ์ด ์กฐ๊ฑด์ด ์—†์œผ๋ฉด ์ž ๊ธด ์„ฌ๋“ค๋„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•œ๋‹ค.
        q = deque([(i, j)])
        visited[i][j] = True
        cnt += 1

        while q:
          xi, xj = q.popleft()
          for mi, mj in move:
            ci, cj = xi + mi, xj + mj
            if (0<=ci<n) and (0<=cj<n):
              if (not visited[ci][cj]) and (land[ci][cj] > num):
              # ์ƒํ•˜์ขŒ์šฐ ์ด๋™ํ•˜๋ฉด์„œ ๋ฐฉ๋ฌธํ•œ์  ์—†๊ณ  ๋ฒ”์œ„ ๋‚ด์— ์žˆ์„ ๋•Œ ์ด๋™
                visited[ci][cj] = True
                q.append((ci, cj))
                
                
  result.append(cnt)

print(max(result)) # ์ œ์ผ ์„ฌ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋งŽ์„ ๋•Œ ์ถœ๋ ฅ
profile
์Šคํƒฏ ์˜ฌ๋ฆฌ๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€