728x90
반응형
문제
https://www.acmicpc.net/problem/2468
문제 풀이
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
반응형
'IT > 알고리즘' 카테고리의 다른 글
[백준] 4963 파이썬(python) (0) | 2022.01.08 |
---|---|
[백준] 1697 파이썬(python) (0) | 2022.01.08 |
[백준] 11403 파이썬(python) (0) | 2022.01.08 |
[백준] 9372 파이썬(python) (0) | 2022.01.08 |
[백준] 7569 파이썬(python) (0) | 2022.01.08 |