[백준 2468 파이썬] 안전 영역

일단 해볼게·2023년 3월 17일
0

백준

목록 보기
104/132

https://www.acmicpc.net/problem/2468

import sys
input = sys.stdin.readline
from collections import deque

n = int(input().rstrip())
area = [list(map(int, input().rstrip().split())) for _ in range(n)] # 지역 리스트

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def bfs(height, start_x, start_y):
    visited[start_x][start_y] = True
    q = deque()
    q.append((start_x, start_y))
    
    while q:
        x, y = q.popleft()
        
        for i in range(4): # 동서남북 탐색
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < n and 0 <= ny < n:
                if not visited[nx][ny] and area[nx][ny] > height: # 높이보다 커서 물에 잠기지 않을 때
                    q.append((nx, ny))
                    visited[nx][ny] = True
                    
max_height = 0
cnt = 0 # 안전영역 개수

for i in range(n): # 최대 높이 범위 구하기
    for j in range(n):
        max_height = max(area[i][j], max_height)

for height in range(max_height):
    visited = [[0] * n for _ in range(n)]
    temp_cnt = 0

    for a in range(n):
        for b in range(n):
            if area[a][b] > height and not visited[a][b]:
                bfs(height, a, b)
                temp_cnt += 1 # height에 따른 안전영역 개수
    
    cnt = max(cnt, temp_cnt) # 안전영역 개수가 많은 것 저장

print(cnt)
    
  • 처음에 지역 좌표가 str이라 int로 형변환해야한다.
  • for height in range(max_height): 에서 범위를 최소 높이, 최대 높이로 설정하니 90%에서 틀렸다고 한다. 이유를 모르겠다.
profile
시도하고 More Do하는 백엔드 개발자입니다.

0개의 댓글