πŸ—οΈ Algorithm/🟩 λ°±μ€€

🟩 [λ°±μ€€] [Python] 2468번_μ•ˆμ „ μ˜μ—­

Dbswnstjd 2022. 11. 9. 03:18

문제

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

 

2468번: μ•ˆμ „ μ˜μ—­

μž¬λ‚œλ°©μž¬μ²­μ—μ„œλŠ” λ§Žμ€ λΉ„κ°€ λ‚΄λ¦¬λŠ” μž₯λ§ˆμ² μ— λŒ€λΉ„ν•΄μ„œ λ‹€μŒκ³Ό 같은 일을 κ³„νšν•˜κ³  μžˆλ‹€. λ¨Όμ € μ–΄λ–€ μ§€μ—­μ˜ 높이 정보λ₯Ό νŒŒμ•…ν•œλ‹€. κ·Έ λ‹€μŒμ— κ·Έ 지역에 λ§Žμ€ λΉ„κ°€ 내렸을 λ•Œ 물에 μž κΈ°μ§€ μ•ŠλŠ”

www.acmicpc.net

풀이

# λ°±μ€€ 2468번 문제 - μ•ˆμ „ μ˜μ—­
from collections import deque
n = int(input())
graph = []
dx, dy = [0,0,-1,1],[1,-1,0,0]
graph = [list(map(int, input().split())) for _ in range(n)]
# 2 <= n ν–‰ <= 100 
# 1 <= i 높이 <= 100
# i = 2 ~ max(graph) - 1

# 1 λΆ€ν„° κ·Έλž˜ν”„μ˜ (μ΅œλŒ€ 높이-1)κΉŒμ§€ ν™•μΈν•˜κΈ° μœ„ν•΄
max_height = []
for i in range(len(graph)):
    max_height.append(max(graph[i]))
max_height = max(max_height)

count = 0

def bfs(x, y, k):
    count = 1
    queue = deque()
    queue.append((x,y))
    while queue:
        x, y = queue.popleft()
        visited[x][y] = 1 # 방문 처리
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < n and 0 <= ny < n:
                if graph[nx][ny] > k and visited[nx][ny] == 0:
                    queue.append((nx,ny))
                    count += 1
                    visited[nx][ny] = 1          
    return count
res = 0
for k in range(max_height + 1):
    visited = [[0]*n for _ in range(n)]
    res_cnt = []
    for i in range(n):
        for j in range(n):
            print('I: ', i,' J: ', j, ' K', k)
            if graph[i][j] > k and visited[i][j] == 0:
                res_cnt.append(bfs(i,j,k))
                count = 0
    res = max(res, len(res_cnt))
print(res)