๐Ÿงฉ Problem Solving/[๋ฐฑ์ค€]

[๋ฐฑ์ค€] 16234 ์ธ๊ตฌ์ด๋™ (python ํŒŒ์ด์ฌ)

์ œ๋ด‰์•„ 2022. 6. 21. 15:22

https://www.acmicpc.net/problem/16234

 

16234๋ฒˆ: ์ธ๊ตฌ ์ด๋™

N×Nํฌ๊ธฐ์˜ ๋•…์ด ์žˆ๊ณ , ๋•…์€ 1×1๊ฐœ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋•…์—๋Š” ๋‚˜๋ผ๊ฐ€ ํ•˜๋‚˜์”ฉ ์กด์žฌํ•˜๋ฉฐ, rํ–‰ c์—ด์— ์žˆ๋Š” ๋‚˜๋ผ์—๋Š” A[r][c]๋ช…์ด ์‚ด๊ณ  ์žˆ๋‹ค. ์ธ์ ‘ํ•œ ๋‚˜๋ผ ์‚ฌ์ด์—๋Š” ๊ตญ๊ฒฝ์„ ์ด ์กด์žฌํ•œ๋‹ค. ๋ชจ

www.acmicpc.net


์•„์ด๋””์–ด

1. ํŒŒ์ด์ฌ ์ด์Šˆ

  • ์ฒ˜์Œ์— dfs๋กœ ์ ‘๊ทผํ–ˆ๋Š”๋ฐ Python3๋กœ ์ฑ„์ ํ•˜๋ฉด 80% ์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. (pypy๋กœ๋Š” ํ†ต๊ณผ)
  • ํฌ๊ธฐํ•˜๊ณ  bfs๋กœ ์ ‘๊ทผํ–ˆ๋Š”๋ฐ ๋˜ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์กฐ๊ฑด์„ ๋ช‡ ๊ฐœ ์ถ”๊ฐ€ํ•˜๋‹ˆ ํ†ต๊ณผ๋จ.
  • c++ ์˜€์œผ๋ฉด dfs๋กœ ํ†ต๊ณผํ–ˆ์„ ๊ฑฐ ๊ฐ™๋‹ค.

 

2. ๊ตฌํ˜„ ํŒŒํŠธ๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐœ๋กœ ๋‚˜๋ˆด๋‹ค.

  • ๊ตญ๊ฒฝ์„  ์—ด๊ธฐ
  • ์ธ๊ตฌ์ˆ˜ ๋ถ„๋ฐฐ

์ฝ”๋“œ ์„ค๋ช…

๊ธฐ๋ณธ์ ์œผ๋กœ ์ด์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๊ฐ ์œ„์น˜๋งˆ๋‹ค bfs๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ๊ฑฐ๋‚˜ checkํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด bfs๋ฅผ ์‹คํ–‰ํ• ์ง€ ๋ง์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.

์ธ๊ตฌ์ด๋™์ด ์žˆ์„ ๋•Œ๊นŒ์ง€ ๊ณ„์† ๋ฐ˜๋ณตํ•ด์•ผ ํ•˜๋ฏ€๋กœ population_flag๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ธ๊ตฌ๋ณ€ํ™”๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.

 

def bfs(x,y):
    global population_flag
    union_list = []
    union_person = 0
    deq = deque()

    visited[x][y] = True
    union_list.append([x,y])
    union_person += country[x][y]
    deq.append([x,y])

    while deq:
        a,b = deq.popleft()
        for i in range(4):
            nx, ny = a + dx[i], b + dy[i]
            if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny]:
                if L <= abs(country[a][b] - country[nx][ny]) <= R:
                    visited[nx][ny] = True
                    deq.append([nx,ny])
                    union_list.append([nx,ny])
                    union_person += country[nx][ny]

    union_len = len(union_list)

์—ฐํ•ฉํ•œ ๋‚˜๋ผ๋ฅผ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ์™€ ์—ฐํ•ฉ ์ธ๊ตฌ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค.

์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ์ธ๊ตฌ ์ฐจ์ด ์กฐ๊ฑด์„ ํ™•์ธํ•˜๋ฉฐ bfs ํƒ์ƒ‰ํ•œ๋‹ค.

ํ์— ๋„ฃ์„ ๋•Œ๋งˆ๋‹ค union_list์™€ union_person์„ ์—…๋ฐ์ดํŠธํ•ด์ค€๋‹ค.

 

def check(x, y):
    for i in range(4):
        nx, ny = x + dx[i], y + dy[i]
        if 0 <= nx < N and 0 <= ny < N:
            if L <= abs(country[x][y] - country[nx][ny]) <= R:
                return 1
    return 0

์ด ํ•จ์ˆ˜๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€๋กœ ๋งŒ๋“  ํ•จ์ˆ˜๋‹ค. ์—ฐํ•ฉ๊ตญ๊ฐ€๊ฐ€ 1๊ฐœ๋ผ๋ฉด bfsํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ

๋ฏธ๋ฆฌ ์ฃผ์œ„ ๊ตญ๊ฐ€์™€ ์ธ๊ตฌ์ˆ˜ ์ฐจ์ด๋ฅผ ํŒ๋‹จํ•ด ๋ฆฌํ„ด์„ ํ•ด์ค€๋‹ค.

 


์ „์ฒด ์ฝ”๋“œ

import sys
from collections import deque
N, L, R = map(int,input().split())
country = []
dx = [1,-1,0,0]
dy = [0,0,1,-1]
population_flag = True
result = 0

for _ in range(N):
    country.append([int(x) for x in sys.stdin.readline().rstrip().split()])

def bfs(x,y):
    global population_flag
    union_list = []
    union_person = 0
    deq = deque()

    visited[x][y] = True
    union_list.append([x,y])
    union_person += country[x][y]
    deq.append([x,y])

    while deq:
        a,b = deq.popleft()
        for i in range(4):
            nx, ny = a + dx[i], b + dy[i]
            if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny]:
                if L <= abs(country[a][b] - country[nx][ny]) <= R:
                    visited[nx][ny] = True
                    deq.append([nx,ny])
                    union_list.append([nx,ny])
                    union_person += country[nx][ny]

    union_len = len(union_list)

    if union_len >= 2:
        for u in union_list:
            country[u[0]][u[1]] = union_person // union_len
        population_flag = True

def check(x, y):
    for i in range(4):
        nx, ny = x + dx[i], y + dy[i]
        if 0 <= nx < N and 0 <= ny < N:
            if L <= abs(country[x][y] - country[nx][ny]) <= R:
                return 1
    return 0

while population_flag:
    population_flag = False
    visited = [[False] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if not visited[i][j] and check(i,j):
                bfs(i,j)

    if population_flag:
        result += 1

print(result)

 

์•„๋ž˜๋Š” dfs๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

import sys
sys.setrecursionlimit(10**4)
N, L, R = map(int,input().split())
country = []
dx = [1,-1,0,0]
dy = [0,0,1,-1]

for _ in range(N):
    country.append([int(x) for x in sys.stdin.readline().rstrip().split()])
visited = [[False]*N for _ in range(N)]

def dfs(x,y):
    global union_person
    visited[x][y] = True
    union_list.append([x,y])
    union_person += country[x][y]

    for i in range(4):
        nx, ny = x + dx[i], y + dy[i]
        if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny]:
            if L <= abs(country[x][y] - country[nx][ny]) <= R:
                dfs(nx,ny)

population_flag = True
result = 0

while population_flag:
    population_flag = False
    visited = [[False] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if not visited[i][j]:
                union_list = []
                union_person = 0
                dfs(i,j)
                union_len = len(union_list)

                if union_len >= 2:
                    population_flag = True

                for u in union_list:
                    country[u[0]][u[1]] = union_person//union_len

    if population_flag:
        result += 1

print(result)