Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Created July 12, 2025 04:24
Show Gist options
  • Save inspirit941/7ffcf491e45b79d0c4ce6ede065a9c56 to your computer and use it in GitHub Desktop.
Save inspirit941/7ffcf491e45b79d0c4ce6ede065a9c56 to your computer and use it in GitHub Desktop.
from collections import deque
import sys
n = int(sys.stdin.readline())
maps =[]
dirs = [(-1,0),(1,0),(0,-1),(0,1)]
max_value = 0
for _ in range(n):
arr = list(map(int, sys.stdin.readline().split()))
max_value = max(max_value, max(arr))
maps.append(arr)
def find_island(maps, visited, start, threshold):
queue = deque()
queue.append(start)
while queue:
y, x = queue.popleft()
visited[y][x] = 1
for dy, dx in dirs:
if 0 <= y+dy < n and 0 <= x+dx < n and visited[y+dy][x+dx] == 0 and maps[y+dy][x+dx] > threshold:
visited[y+dy][x+dx] = 1
queue.append((y+dy, x+dx))
return 1
answer = 0
for t in range(max_value):
visited = [[0 for _ in range(n)] for _ in range(n)]
result = 0
for y in range(n):
for x in range(n):
if visited[y][x] == 0 and maps[y][x] > t:
result += find_island(maps, visited, (y, x), t)
answer = max(answer, result)
print(answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment