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

2022. 6. 21. 15:22ยท๐Ÿงฉ Problem Solving/[๋ฐฑ์ค€]

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)

'๐Ÿงฉ Problem Solving > [๋ฐฑ์ค€]' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[๋ฐฑ์ค€] 18111 ๋งˆ์ธํฌ๋ž˜ํ”„ํŠธ (python ํŒŒ์ด์ฌ)  (4) 2022.06.23
[๋ฐฑ์ค€] 1107 ๋ฆฌ๋ชจ์ฝ˜ (python ํŒŒ์ด์ฌ)  (0) 2022.06.22
[๋ฐฑ์ค€] 1389 ์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™ (python ํŒŒ์ด์ฌ)  (0) 2022.06.19
[๋ฐฑ์ค€] 7562 ๋‚˜์ดํŠธ์˜ ์ด๋™ (python ํŒŒ์ด์ฌ)  (0) 2022.06.18
[๋ฐฑ์ค€] 11404 ํ”Œ๋กœ์ด๋“œ (python ํŒŒ์ด์ฌ)  (0) 2022.06.18
'๐Ÿงฉ Problem Solving/[๋ฐฑ์ค€]' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [๋ฐฑ์ค€] 18111 ๋งˆ์ธํฌ๋ž˜ํ”„ํŠธ (python ํŒŒ์ด์ฌ)
  • [๋ฐฑ์ค€] 1107 ๋ฆฌ๋ชจ์ฝ˜ (python ํŒŒ์ด์ฌ)
  • [๋ฐฑ์ค€] 1389 ์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™ (python ํŒŒ์ด์ฌ)
  • [๋ฐฑ์ค€] 7562 ๋‚˜์ดํŠธ์˜ ์ด๋™ (python ํŒŒ์ด์ฌ)
์ œ๋ด‰์•„
์ œ๋ด‰์•„
  • ์ œ๋ด‰์•„
    Overthinking
    ์ œ๋ด‰์•„
    fake it till you make it.
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (106)
      • ๐Ÿงฉ Problem Solving (83)
        • [๋ฐฑ์ค€] (74)
        • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (7)
        • [SW Expert Academy] (1)
        • [์•Œ๊ณ ๋ฆฌ์ฆ˜ for PS] (1)
      • ๐Ÿ“ฆ Data Structure (2)
      • ๐Ÿ“œ Language (14)
        • [python] (14)
      • ๐Ÿ–ค Git (1)
      • ๐ŸŒ† ์ผ์ƒ (4)
        • ๐Ÿ’ฌ ๋ฒฝ๋ณด๊ณ  ๋งํ•˜๊ธฐ (4)
      • ๐Ÿ—„๏ธ ๊ธฐํƒ€ (2)
      • ๐Ÿ”ต css (0)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ํŒฐ๋ฆฐ๋“œ๋กฌ
    ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ
    slicing
    ๋ถ„ํ•  ์ •๋ณต
    ํŒŒ์ด์ฌ
    ๋ธŒ๋ฃจํŠธํฌ์Šค
    boj
    BFS
    ๋ฐฑ์ค€
    ๋ฐฑํŠธ๋ž˜ํ‚น
    ์ •์ฒ˜๊ธฐ
    ์Šคํƒ
    ํˆฌํฌ์ธํ„ฐ
    DP
    ์œ„์ƒ์ •๋ ฌ
    ๊ตฌํ˜„
    Python
    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    ๋ƒ…์ƒ‰
    DFS
    ํ”Œ๋กœ์ด๋“œ์›Œ์…œ
    ๋ˆ„์ ํ•ฉ
    SWEA
    ๋ถ€๋ถ„ํ•ฉ
    imos
    Bruteforce
    ๋ฐ๋ธŒ์ฝ”์Šค
    ๊ทธ๋ฆฌ๋””
    ๋‹ค์ต์ŠคํŠธ๋ผ
    ์žฌ๊ท€
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
์ œ๋ด‰์•„
[๋ฐฑ์ค€] 16234 ์ธ๊ตฌ์ด๋™ (python ํŒŒ์ด์ฌ)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”