IT/알고리즘

[백준] 2468 파이썬(python)

상쾌한기분 2022. 1. 8. 16:43
728x90
반응형

문제

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

 

2468번: 안전 영역

재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는

www.acmicpc.net

 

문제 풀이

import collections
import sys

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

N = int(sys.stdin.readline().strip())
MAX_VALUE, MIN_VALUE = 0, 0
maps = []
for _ in range(N):
    case = list(map(int, sys.stdin.readline().split()))
    MIN_VALUE = min(min(case), MIN_VALUE)
    MAX_VALUE = max(max(case), MAX_VALUE)
    maps.append(case)

def bfs(graph, y, x, rain, visited):
    visited[y][x] = 1
    queue = collections.deque([(y, x)])

    while queue:
        yy, xx = queue.popleft()

        for i in range(4):
            nx = xx + dx[i]
            ny = yy + dy[i]
            if 0 <= nx < N and 0 <= ny < N and graph[ny][nx] > rain and visited[ny][nx] == 0:
                visited[ny][nx] = 1
                queue.append((ny, nx))

results = []
for rain in range(MIN_VALUE, MAX_VALUE):
    count = 0
    visited = [[0] * N for _ in range(N)]

    for y in range(N):
        for x in range(N):
            if maps[y][x] > rain and visited[y][x] == 0:
                bfs(maps, y, x, rain, visited)
                count += 1

    results.append(count)
print(max(results))
728x90
반응형