ํŒŒ์ด์ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€ 16234] ์ธ๊ตฌ ์ด๋™ (Python)

์€์ง„ 2021. 8. 10. 21:50

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[๋ฐฑ์ค€ 16234] ์ธ๊ตฌ ์ด๋™ (Python)
๋ฐฑ์ค€


โœ๏ธIdea Sketch

2021-08-09

1. ์ด๋ฒˆ ๋ฌธ์ œ๋Š” 80% ์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ด ์• ๋ฅผ ๋จน์€ ๋ฌธ์ œ๋‹ค.

  • Python3์—๋Š” ์•ฝ 1000ํšŒ์˜ ์žฌ๊ท€ ์ œํ•œ์ด ์žˆ๋‹ค.
  • PyPy3๋Š” ์žฌ๊ท€ ์ œํ•œ ํšŸ์ˆ˜๊ฐ€ ๋” ์ปค์„œ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
import sys
sys.setrecursionlimit(10**6)

2. BFS์™€ DFS

  • DFS, BFS ๋‘˜ ์ค‘ ์–ด๋Š ๊ฒƒ์„ ํ•ด๋„ ์ƒ๊ด€์—†์ง€๋งŒ
  • BFS๋กœ ๊ตฌํ˜„ํ•œ ์‚ฌ๋ก€๊ฐ€ ๋งŽ๋‹ค.
  • ์ด๋ฒˆ์— DFS๋กœ ๊ตฌํ˜„ํ•ด๋ดค๋Š”๋ฐ ์žฌ๊ท€ ์ œํ•œ์œผ๋กœ ์• ๋ฅผ ๋งŽ์ด ๋จน์—ˆ๋‹ค.
  • ๋‹ค์‹œ ์ด ๋ฌธ์ œ๋ฅผ ๋ณธ๋‹ค๋ฉด BFS๋กœ ๊ตฌํ˜„ํ•ด๋ณผ ๊ฒƒ.

3. ๋กœ์ง ์ดํ•ดํ•˜๊ธฐ

  • ๋กœ์ง ์ดํ•ด์— ๋งŽ์€ ๋„์›€์ด ๋œ ๋งˆ์ด๊ตฌ๋ฏธ๋‹˜ ๋ธ”๋กœ๊ทธ
  • dfs(x, y) : ์ƒํ•˜์ขŒ์šฐ ์ธ์ ‘ํ•œ ๋‚˜๋ผ์™€ ์ธ๊ตฌ ์ฐจ์ด๋ฅผ ํ™•์ธํ•œ๋‹ค.
  • flag : ์ธ์ ‘ํ•œ ๋‚˜๋ผ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • main : ์—ฐํ•ฉํ•œ ๋‚˜๋ผ์˜ ์ขŒํ‘œ group๊ณผ ์ด ์ธ๊ตฌ ์ˆ˜ sum์„ ์ •์˜ํ•œ๋‹ค.
  • exit
    1) ์ธ์ ‘ํ•œ ๋‚˜๋ผ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ False, graph์— ์ธ๊ตฌ ์ด๋™์„ ๋ฐ˜์˜ํ•œ๋‹ค.
    2) ์ธ์ ‘ํ•œ ๋‚˜๋ผ๊ฐ€ ์—†๊ณ  graph ํƒ์ƒ‰์„ ๋๋‚ธ ๊ฒฝ์šฐ True, while๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-08-09

  • Python3๋กœ ์ œ์ถœํ•  ๊ฒฝ์šฐ 80%์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ
  • PyPy3๋กœ ์ œ์ถœํ•  ๊ฒฝ์šฐ 900ms ํ†ต๊ณผ
import sys

def dfs(x, y):
  global sum
  flag = False
  if (0<=x<n) and (0<=y<n):
    visit[x][y] = 1
    if (0<=x-1<n) and (0<=y<n):
      if visit[x-1][y] == 0:
        if l <= abs(graph[x][y] - graph[x-1][y]) <= r:
          group.append([x-1, y])
          sum += graph[x-1][y]
          flag = True
          dfs(x-1, y)
    if (0<=x+1<n) and (0<=y<n):
      if visit[x+1][y] == 0:
        if l <= abs(graph[x][y] - graph[x+1][y]) <= r:
          group.append([x+1, y])
          sum += graph[x+1][y]
          flag = True
          dfs(x+1, y)
    if (0<=x<n) and (0<=y-1<n):
      if visit[x][y-1] == 0:
        if l <= abs(graph[x][y] - graph[x][y-1]) <= r:
          group.append([x, y-1])
          sum += graph[x][y-1]
          flag = True
          dfs(x, y-1)
    if (0<=x<n) and (0<=y+1<n):
      if visit[x][y+1] == 0:
        if l <= abs(graph[x][y] - graph[x][y+1]) <= r:
          group.append([x, y+1])
          sum += graph[x][y+1]
          flag = True
          dfs(x, y+1)
  return (group, sum, flag)


if __name__ == '__main__':
  n, l, r = map(int, input().split())
  graph = [list(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]
  result = 0

  while True:
    visit = [[0 for _ in range(n)] for _ in range(n)]
    result += 1
    exit = True
    for i in range(n):
      for j in range(n):
        if visit[i][j] == 0:
          group = [[i, j]]
          sum = graph[i][j]
          group, sum, flag = dfs(i, j)
          if flag == True:
            exit = False
            for x, y in group:
              graph[x][y] = int(sum/len(group))
    if exit == True:
      print(result-1)
      break


โœ๏ธ๋ช…๋‹ต

BFS ๋กœ์ง

import sys
from collections import deque
input = sys.stdin.readline


dy = [1, -1, 0, 0] 
dx = [0, 0, 1, -1]


def bfs(y, x):
    global visisted, m
    queue = deque()
    queue.append((y, x))
    sum_val = m[y][x]
    visisted[y][x] = 1
    pos = [(y, x)]

    while queue:
        y, x = queue.popleft()

        for my, mx in zip(dy, dx):
            ny = y + my
            nx = x + mx
            if 0 <= ny < N and 0 <= nx < N and not visisted[ny][nx]:
                if L <= abs(m[ny][nx] - m[y][x]) <= R:
                    visisted[ny][nx] = 1
                    sum_val += m[ny][nx]
                    queue.append((ny, nx))
                    pos.append((ny, nx))

    if len(pos) > 1:  # exist group
        cnt = len(pos)
        mean = sum_val // cnt
        for i in range(cnt):
            y, x = pos[i] 
            m[y][x] = mean
            q.append((y, x))
        return 0
    else:
        return 1


def check(y, x):
    for my, mx in zip(dy, dx):
        ny = y + my
        nx = x + mx
        if 0 <= ny < N and 0 <= nx < N:
            if L <= abs(m[ny][nx] - m[y][x]) <= R:
                return 0
    return 1


N, L, R = map(int, input().split())
m = [[] for _ in range(N)]
q = deque()
for i in range(N):
    m[i] = list(map(int, input().split()))
    for j in range(N):
        q.append((i, j))

cnt = 0
flag = False 

while not flag:
    visisted = [[0] * N for _ in range(N)]
    flag = True
    size = len(q)
    for _ in range(size):
        y, x = q.popleft()
        if visisted[y][x] or check(y, x):
            continue
        if not bfs(y, x):
            flag = False 
    if not flag:
        cnt += 1
print(cnt)