-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
18-SeongHoonC #70
18-SeongHoonC #70
Conversation
μ λ λκ°μ μκ³ λ¦¬μ¦μΌλ‘ νμλλ° from collections import deque
import sys
def BFS(y,x) :
queue = deque()
result = []
queue.append((x,y))
result.append((x,y))
while queue :
x,y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < N and visited[ny][nx] == 0:
if L <= abs(ground[y][x] - ground[ny][nx]) <= R: # L,Rλ‘ λλ§Ή μ°ΎκΈ°
visited[ny][nx] = 1
queue.append((nx,ny))
result.append((nx,ny))
return result
dx = [0,0,-1,1]
dy = [1,-1,0,0]
N,L,R = map(int, input().split())
ground = [list(map(int, input().split())) for _ in range(N)]
day = 0
while 1:
visited = [[0] * (N+1) for _ in range(N+1)]
stop = 0
for i in range(N):
for j in range(N):
if visited[i][j] == 0:
visited[i][j] = 1
result = BFS(i,j) # λλ§Ήμ λ§Ίμ λλΌ κ°μ Έμ€κΈ°
if len(result) > 1: # λλ§Ήμ΄ μμΌλ©΄ νκ· κ° λ£κΈ°
stop = 1
num = sum([ground[y][x] for x, y in result]) // len(result)
for x,y in result:
ground[y][x] = num
if stop == 0: # λλ§Ήμ λ§Ίμ μ μλ€λ©΄
break
day += 1
print(day) |
// μ°ν©λ€μ νκ· κ°μΌλ‘ λ³κ²½ | ||
private fun goAverage(grounds: Array<Array<Int>>, unity: List<Pair<Int, Int>>) { | ||
val average = unity.sumOf { grounds[it.first][it.second] } / unity.size | ||
unity.forEach { | ||
grounds[it.first][it.second] = average | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νκ· κ°κΉμ§ ν¨μννμ μ λ§λμ ¨κ΅°μ!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BFS μ°μ΅μ©μΌλ‘ μ’μ λ¬Έμ κ°μ¬ν¨λ€! 보면μ νμμ§λ§..! π¨
import sys
from collections import deque
input = sys.stdin.readline
N,L,R = map(int, input().split())
country = [list(map(int, input().split())) for _ in range(N)]
q = deque()
#μ°ν©μ΄ λ μ μμμ§ νμΈ ν μ°λμ΄ λλ©΄ μ μ₯
dx = [1,0,-1,0]
dy = [0,1,0,-1]
def bfs(x,y):
q.append((x,y))
union=[]
union.append((x,y))
while q:
a,b = q.popleft()
for i in range(4):
na = a + dx[i]
nb = b + dy[i]
if na >= N or nb>= N or nb<0 or na <0 or visited[na][nb]==1:
continue
if R>=abs(country[a][b]-country[na][nb]) >= L:
visited[na][nb] = 1
q.append((na,nb))
union.append((na,nb))
if len(union)<=1:
return 0
result=sum(country[a][b] for a,b in union) // len(union)
for a,b in union:
country[a][b] = result
return 1
day=0
while True :
stop = 0
visited = [[0]*N for _ in range(N)]
for i in range(N):
for j in range(N):
if visited[i][j] == 0:
visited[i][j] = 1
stop += bfs(i,j)
if stop ==0:
break
day+=1
print(day)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νκ³ λ³΄λκΉ λ―Όμλμ΄λ κ±°μ λκ°μ΄ ν리λ€μ.. γ
γ
γ
μ¬λ°λ λ¬Έμ λ€μ! νμ΄μ¬μ λλ¦° μλκ° ν¬κ² μλΏλ λ¬Έμ μμ΄μ..
from collections import deque
import sys
input = sys.stdin.readline
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
def bfs(x, y):
union = []
q = deque()
union.append((x,y))
q.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] and L <= abs(_map[x][y] - _map[nx][ny]) <= R:
visited[nx][ny] = True
q.append((nx,ny))
union.append((nx,ny))
return union
N, L, R = map(int, input().split())
_map = []
for _ in range(N):
_map.append(list(map(int, input().split())))
answer = 0
while True:
flag = True
visited = [[False] * N for _ in range(N)]
for i in range(N):
for j in range(N):
if not visited[i][j]:
visited[i][j] = True
union = bfs(i,j)
if len(union) > 1:
flag = False
human = 0
for x, y in union:
human += _map[x][y]
human //= len(union)
for x, y in union:
_map[x][y] = human
if flag:
break
answer += 1
print(answer)
π λ¬Έμ λ§ν¬
μΈκ΅¬ μ΄λ
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
π μλ‘κ² μκ²λ λ΄μ©