문제
코드
from collections import deque
import sys
input = sys.stdin.readline
dx = [-1, 0, 0, 1]
dy = [0, -1, 1, 0]
def bfs(x: int, y: int, standard: int):
queue = deque()
queue.append([x, y])
visited[x][y] = True
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == False and graph[nx][ny] > standard:
queue.append([nx, ny])
visited[nx][ny] = True
if __name__ == '__main__':
n = int(input())
graph = [list(map(int, input().split())) for _ in range(n)]
max_value = max(map(max, graph))
safe = []
for i in range(max_value):
area = 0
visited = [[False] * n for _ in range(n)]
for x in range(n):
for y in range(n):
if graph[x][y] > i and visited[x][y] == False:
bfs(x, y, i)
area += 1
safe.append(area)
print(max(safe))
결과
출처 & 깃허브
프로그래머스
깃허브