해결된 질문
작성
·
240
0
h를 무조건 0~99로 설정하기보다는 미리 높이의 min 값과 max값을 구한 후 min~max 범위로 for 문을 돌려주면 더 효율적인 풀이가 될 것 같습니다.
from collections import deque
N = int(input())
graph = [list(map(int,input().split())) for _ in range(N)]
min_rain = 99999999
max_rain = -1
# 높이의 min,max 구해주기
for row in graph:
min_tmp = min(row)
max_tmp = max(row)
if min_tmp < min_rain:
min_rain = min_tmp
if max_tmp > max_rain:
max_rain = max_tmp
dr = [1,0,-1,0]
dc = [0,1,0,-1]
ans = 0
for rain in range(min_tmp+1,max_rain):
visited = [[0 for _ in range(N)] for _ in range(N)]
cnt = 0
for rr in range(N):
for cc in range(N):
if visited[rr][cc] == 0 and rain < graph[rr][cc] :
visited[rr][cc] = 1
q = deque()
q.append((rr,cc))
while q:
r,c = q.popleft()
for i in range(4):
nr = r + dr[i]
nc = c + dc[i]
if 0 <= nr < N and 0 <= nc < N and visited[nr][nc] == 0 and rain < graph[nr][nc]:
visited[nr][nc] = 1
q.append((nr,nc))
cnt += 1
if cnt > ans:
ans = cnt
print(ans)