import sys
import heapq
import math
from collections import deque
sys.setrecursionlimit(100000)
dx = [-1,1,0,0]
dy = [0,0,-1,1]
N,L,R = map(int,input().split())
pp = [list(map(int,input().split())) for _ in range(N)]
cnt = 0
def go(x,y) :
totSum = pp[x][y]
totGrp = [(x,y)]
for k in range(4):
nx,ny = x+dx[k],y+dy[k]
if 0 <= nx < N and 0 <= ny < N :
if L <= abs(pp[nx][ny] - pp[x][y]) <= R and ch[nx][ny] == False :
ch[nx][ny] = True
tmpSum,tmpGrp = go(nx,ny)
totSum += tmpSum
totGrp += tmpGrp
return totSum,totGrp
while True:
change = False
ch = [[False]*N for _ in range(N)]
befpp = [list(p) for p in pp]
for i in range(N):
for j in range(N):
if ch[i][j] == False:
ch[i][j] = True
psum,pgrp = go(i,j)
pavg = math.floor(psum//len(pgrp))
for gp in pgrp:
x,y = gp
pp[x][y] = pavg
for i in range(N):
for j in range(N):
if befpp[i][j] != pp[i][j]:
change = True
break
if not change :
break
cnt += 1
print(cnt)
반응형
'알고리즘' 카테고리의 다른 글
[알고리즘 Python] 백준 2468 - 안전 영역 (0) | 2021.12.17 |
---|---|
[알고리즘 Python] 프로그래머스 - 상호평가 (0) | 2021.12.17 |
[알고리즘 C++] 백준 2655 - 가장높은탑쌓기 (0) | 2021.12.17 |
[알고리즘 C++] 프로그래머스 - 등굣길 (0) | 2021.12.17 |
[알고리즘 C++] Linked List & Vector (0) | 2021.12.17 |