๋ฌธ์
https://www.acmicpc.net/problem/16234
16234๋ฒ: ์ธ๊ตฌ ์ด๋
N×Nํฌ๊ธฐ์ ๋ ์ด ์๊ณ , ๋ ์ 1×1๊ฐ์ ์นธ์ผ๋ก ๋๋์ด์ ธ ์๋ค. ๊ฐ๊ฐ์ ๋ ์๋ ๋๋ผ๊ฐ ํ๋์ฉ ์กด์ฌํ๋ฉฐ, rํ c์ด์ ์๋ ๋๋ผ์๋ A[r][c]๋ช ์ด ์ด๊ณ ์๋ค. ์ธ์ ํ ๋๋ผ ์ฌ์ด์๋ ๊ตญ๊ฒฝ์ ์ด ์กด์ฌํ๋ค. ๋ชจ
www.acmicpc.net
ํ์ด
# ๋ฐฑ์ค 16234๋ฒ ๋ฌธ์ - ์ธ๊ตฌ ์ด๋
from collections import deque
dx, dy = [0,0,-1,1], [1,-1,0,0]
n, l, r = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(n)]
answer = 0
def bfs(x, y):
q = deque()
q.append((x, y))
union_country = []
union_country.append((x,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 and not visited[nx][ny]:
if l <= abs(graph[x][y] - graph[nx][ny]) <= r:
visited[nx][ny] = True
q.append((nx,ny))
union_country.append((nx, ny))
return union_country
while True:
visited = [[False]*n for _ in range(n)]
move = False
for i in range(n):
for j in range(n):
if not visited[i][j]:
visited[i][j] = True
union_country = bfs(i, j)
if 1 < len(union_country):
move = True
population = sum([graph[x][y] for x, y in union_country]) // len(union_country)
for x, y in union_country:
graph[x][y] = population
if not move:
break
else:
answer += 1
print(answer)
๋ณด๊ธฐ์ ์ฌ์ด ๋ฌธ์ ์์ง๋ง ์์ด๋์ด๊ฐ ์๊ฐ๋์ง ์์ ๋งค์ฐ ํค๋ฉ๋ค๊ฐ ๋ค๋ฅธ ์ฌ๋์ ํ์ด๋ฅผ ๋ณด๊ณ ๊ฒจ์ฐ ํด๊ฒฐํ์๋ค.
์ถ์ฒ
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] 24444๋ฒ_์๊ณ ๋ฆฌ์ฆ ์์ - ๋๋น ์ฐ์ ํ์ 1 (0) | 2022.12.16 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] 24445๋ฒ_์๊ณ ๋ฆฌ์ฆ ์์ - ๋๋น ์ฐ์ ํ์ 2 (0) | 2022.12.16 |
๐ฉ [๋ฐฑ์ค] [Python] 2589๋ฒ_๋ณด๋ฌผ ํ์ (0) | 2022.12.14 |
๐ฉ [๋ฐฑ์ค] [Python] 3273๋ฒ_๋ ์์ ํฉ (0) | 2022.12.14 |
๐ฉ [๋ฐฑ์ค] [Python] [Class5] 2467๋ฒ_์ฉ์ก (0) | 2022.12.08 |