Skip to content
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

Merged
merged 1 commit into from
Apr 2, 2024
Merged

18-SeongHoonC #70

merged 1 commit into from
Apr 2, 2024

Conversation

SeongHoonC
Copy link
Collaborator

πŸ”— 문제 링크

인ꡬ 이동

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

  1. 인ꡬ가 더 이상 이동할 수 없을 λ•ŒκΉŒμ§€ λ°˜λ³΅ν•˜λ©΄μ„œ 카운트 증가
  2. 인ꡬλ₯Ό bfs 둜 μ—°ν•© μ°ΎκΈ° (L 이상 R μ΄ν•˜μ—¬μ•Όν•œλ‹€)
  3. 찾은 μ—°ν•© 리슀트 μ•„μ΄ν…œλ“€μ„ λ‹€ ν‰κ· κ°’μœΌλ‘œ λ°”κΎΌλ‹€.
  4. λ§Œμ•½ 연합을 ν•˜λ‚˜λ„ λͺ»μ°ΎμœΌλ©΄ 인ꡬλ₯Ό 더 이상 μ΄λ™μ‹œν‚¬ 수 μ—†λ‹€ -> μ’…λ£Œ

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@alstjr7437
Copy link
Member

저도 λ˜‘κ°™μ€ μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ ν’€μ—ˆλŠ”λ°
μ„±ν›ˆλ‹˜μ²˜λŸΌ ν•¨μˆ˜ν™”λ₯Ό λ”°λ‘œ ν•˜μ§€λŠ” μ•Šμ•„μ„œ 쑰금 보기 νž˜λ“€μ–΄μ„œ 주석 μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€!!

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)

Comment on lines +46 to +52
// 연합듀을 ν‰κ· κ°’μœΌλ‘œ λ³€κ²½
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
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν‰κ· κ°’κΉŒμ§€ ν•¨μˆ˜ν™”ν•˜μ…”μ„œ λ§Œλ“œμ…¨κ΅°μš”!

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a 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)

Copy link
Member

@fnzksxl fnzksxl left a 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)

@SeongHoonC SeongHoonC merged commit c3e9e04 into main Apr 2, 2024
6 checks passed
@SeongHoonC SeongHoonC deleted the 18-SeongHoonC branch April 2, 2024 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants