๋ฌธ์ œ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


์ฐจ์„ธ๋Œ€ ์˜๋†์ธ ํ•œ๋‚˜๋Š” ๊ฐ•์›๋„ ๊ณ ๋žญ์ง€์—์„œ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋†์•ฝ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋ ค๋ฉด ๋ฐฐ์ถ”๋ฅผ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๋‚˜๋Š” ํ•ด์ถฉ ๋ฐฉ์ง€์— ํšจ๊ณผ์ ์ธ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๋ฅผ ๊ตฌ์ž…ํ•˜๊ธฐ๋กœ ๊ฒฐ์‹ฌํ•œ๋‹ค. ์ด ์ง€๋ ์ด๋Š” ๋ฐฐ์ถ”๊ทผ์ฒ˜์— ์„œ์‹ํ•˜๋ฉฐ ํ•ด์ถฉ์„ ์žก์•„ ๋จน์Œ์œผ๋กœ์จ ๋ฐฐ์ถ”๋ฅผ ๋ณดํ˜ธํ•œ๋‹ค. ํŠนํžˆ, ์–ด๋–ค ๋ฐฐ์ถ”์— ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•œ ๋งˆ๋ฆฌ๋ผ๋„ ์‚ด๊ณ  ์žˆ์œผ๋ฉด ์ด ์ง€๋ ์ด๋Š” ์ธ์ ‘ํ•œ ๋‹ค๋ฅธ ๋ฐฐ์ถ”๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์–ด, ๊ทธ ๋ฐฐ์ถ”๋“ค ์—ญ์‹œ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ํ•œ ๋ฐฐ์ถ”์˜ ์ƒํ•˜์ขŒ์šฐ ๋„ค ๋ฐฉํ–ฅ์— ๋‹ค๋ฅธ ๋ฐฐ์ถ”๊ฐ€ ์œ„์น˜ํ•œ ๊ฒฝ์šฐ์— ์„œ๋กœ ์ธ์ ‘ํ•ด์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

ํ•œ๋‚˜๊ฐ€ ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋Š” ๋•…์€ ๊ณ ๋ฅด์ง€ ๋ชปํ•ด์„œ ๋ฐฐ์ถ”๋ฅผ ๊ตฐ๋ฐ๊ตฐ๋ฐ ์‹ฌ์–ด ๋†“์•˜๋‹ค. ๋ฐฐ์ถ”๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ๊ณณ์—๋Š” ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•œ ๋งˆ๋ฆฌ๋งŒ ์žˆ์œผ๋ฉด ๋˜๋ฏ€๋กœ ์„œ๋กœ ์ธ์ ‘ํ•ด์žˆ๋Š” ๋ฐฐ์ถ”๋“ค์ด ๋ช‡ ๊ตฐ๋ฐ์— ํผ์ ธ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•˜๋ฉด ์ด ๋ช‡ ๋งˆ๋ฆฌ์˜ ์ง€๋ ์ด๊ฐ€ ํ•„์š”ํ•œ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์ถ”๋ฐญ์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด ์ตœ์†Œ 5๋งˆ๋ฆฌ์˜ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•„์š”ํ•˜๋‹ค. 0์€ ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ์ง€ ์•Š์€ ๋•…์ด๊ณ , 1์€ ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ๋Š” ๋•…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

 

๐Ÿ’ก ์ž…๋ ฅ


์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ์ฒซ์งธ ์ค„์—๋Š” ๋ฐฐ์ถ”๋ฅผ ์‹ฌ์€ ๋ฐฐ์ถ”๋ฐญ์˜ ๊ฐ€๋กœ๊ธธ์ด M(1 ≤ M ≤ 50)๊ณผ ์„ธ๋กœ๊ธธ์ด N(1 ≤ N ≤ 50), ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ๋Š” ์œ„์น˜์˜ ๊ฐœ์ˆ˜ K(1 ≤ K ≤ 2500)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ K์ค„์—๋Š” ๋ฐฐ์ถ”์˜ ์œ„์น˜ X(0 ≤ X ≤ M-1), Y(0 ≤ Y ≤ N-1)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฐฐ์ถ”์˜ ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

 

 

 

๐Ÿ“Œ ์ถœ๋ ฅ


๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ์˜ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด ๋งˆ๋ฆฌ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


์šฐ์„  ๋ฐฐ์ถ”๋ฐญ์„ 0์œผ๋กœ ๋‹ค ์ฑ„์›Œ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ์™€ ๊ฐ™์€ ํฌ๊ธฐ๋กœ false ์ธ์ง€ true์ธ์ง€๋ฅผ ๋‹ด์•„์ค„ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค. ( ์™ผ์ชฝ ์ƒ๋‹จ์ด (0, 0)์ด ๋˜๊ฒŒ )

array=[[0]*m for _ in range(n)]
visited = [[False]*m for _ in range(n)]

 

๊ทธ๋ฆฌ๊ณ  k๋ฒˆ์”ฉ ๋Œ๋ฉด์„œ x, y ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋ฐ›์•„์ค€๋‹ค.

for _ in range(k):
    y, x = map(int,input().split())
    array[x][y] = 1

 

๊ฐ€๋กœ๋กœ m๊นŒ์ง€ ์„ธ๋กœ๋กœ n๊นŒ์ง€ for๋ฌธ์„ ๋Œ๋ฉด์„œ array์™€ visited๋ฅผ ํ†ตํ•ด ๋ฐฐ์ถ”๋Š” ์‹ฌ์–ด์ ธ์žˆ์ง€๋งŒ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์„ ์ฐพ์•„ ๋ฐฉ๋ฌธํ•ด์ฃผ๋Š” dfs๋‚˜ bfs๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด๋œ๋‹ค.

for i in range(n):
    for j in range(m):
        if array[i][j] and not visited[i][j]:
            dfs(i,j) ๋‚˜ bfs(i,j)

 

 

1. DFS๋กœ ํ’€ ๊ฒฝ์šฐ

์žฌ๊ท€ ์šฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. 

 

ํŒŒ์ด์ฌ์˜ ์žฌ๊ท€ ๊นŠ์ด ์ œํ•œ์€ 1000์œผ๋กœ ๋งค์šฐ ์–•๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๋ฌธ์ œ๋ฅผ ๊ทธ๋ƒฅ ํ‘ผ๋‹ค๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

ํŒŒ์ด์ฌ์œผ๋กœ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด ํ’€์–ด์•ผํ•˜๋Š” ๋ฌธ์ œ๋ผ๋ฉด setrecursionlimit๋ฅผ ์“ฐ๋Š” ๊ฒƒ์€ ์„ ํƒ์ด ์•„๋‹Œ ํ•„์ˆ˜๋‹ค.

 

  • ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ์ •ํ•ด์ฃผ๊ณ  ์ด๋ฅผ for๋ฌธ์œผ๋กœ ๋Œ๋ฆฐ๋‹ค.
  • ๊ฐ ์œ„์น˜์˜ ๊ฐ’์ด 0๋ณด๋‹ค ํฌ๊ณ  ์ตœ๋Œ€ ๊ธธ์ด๋ณด๋‹ค ์ž‘์€์ง€ 1์ฐจ์ ์œผ๋กœ ํ™•์ธ์„ ํ•ด์ฃผ๊ณ , ์ด์— ํ•ด๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด continue๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค์Œ ์œ„์น˜๋กœ ๋„˜๊ฒจ์ฃผ๊ฒŒ for๋ฌธ์œผ๋กœ ๋˜์ง„๋‹ค.
  • ๋งž๋‹ค๋ฉด ์ด์   ํ™•์ธ์„ ํ•ด์ค€๋‹ค. ์ด๊ณณ์— ๋ฐฐ์ถ”๊ฐ€ ์žˆ๊ณ  ๋ฐฉ๋ฌธํ•œ ์  ์ด ์—†๋Š”๊ฐ€?

 

# ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
# ๋งŒ์•ฝ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด์•ผํ•˜๋Š” ๋ฌธ์ œ๋ผ๋ฉด setrecursionlimit์„ ์“ฐ๋Š” ๊ฒƒ์€ ์„ ํƒ์ด ์•„๋‹Œ ํ•„์ˆ˜๋‹ค.
# ํŒŒ์ด์ฌ์˜ ๊ธฐ๋ณธ ์žฌ๊ท€ ๊นŠ์ด ์ œํ•œ์€ 1000์œผ๋กœ ๋งค์šฐ ์–•๋‹ค.
# ๋”ฐ๋ผ์„œ ์žฌ๊ท€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๊ฒฝ์šฐ ์ด์ œํ•œ์— ๊ฑธ๋ ค ์—๋Ÿฌ๋ฉ”์„ธ์ง€๋ฅผ ๋ณผ์ˆ˜ ์—†๋‹ค.
# ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.
import sys
sys.setrecursionlimit(100000)

def dfs(x,y):
    visited[x][y] = True
    directions = [ (-1, 0), (1, 0), (0, -1), (0, 1)]
    # ์ƒํ•˜ ์ขŒ์šฐ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ์ง€์ •ํ•ด์ฃผ๊ณ  ์—ฌ๊ธฐ์„œ for๋ฌธ์„ ๋Œ๋ฆฐ๋‹ค.
    for dx, dy in directions:
        nx, ny = dx + x, dy+y
        if nx < 0 or nx >=n or ny<0 or ny>=m:
            continue
        if array[nx][ny] and not visited[nx][ny]:
            dfs(nx,ny)

for _ in range(int(input())):
    m, n, k = map(int,input().split())
    array=[[0]*m for _ in range(n)]
    visited = [[False]*m for _ in range(n)]
    for _ in range(k):
        y, x = map(int,input().split())
        array[x][y] = 1
    result = 0
    for i in range(n):
        for j in range(m):
            if array[i][j] and not visited[i][j]:
                dfs(i,j)
                result += 1
    print(result)

 

 

 

 

2. BFS๋กœ ํ’€์–ด๋ณด๋ฉด,,,

BFS๋Š” ๋ฑ..deque 

 

dfs์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ์ƒํ•˜์ขŒ์šฐ์˜ ์œ„์น˜๋ฅผ ๋‹ด์•„ for๋ฌธ์„ ๋Œ๋ ค์ค„ ๊ฒƒ์ด๋‹ค. 

from collections import deque

def bfs(x,y):
    q = deque()
    q.append((x,y))
    visited[x][y] = True
    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    while q:
        x, y = q.popleft()
        for dx, dy in directions:
            nx, ny = x+dx, y+dy
            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if array[nx][ny] and not visited[nx][ny]:
                q.append((nx,ny))
                visited[nx][ny]=True


for _ in range(int(input())):
    m, n, k = map(int,input().split())
    array=[[0]*m for _ in range(n)]
    visited = [[False]*m for _ in range(n)]
    for _ in range(k):
        y, x = map(int,input().split())
        array[x][y] = 1
    result = 0
    for i in range(n):
        for j in range(m):
            if array[i][j] and not visited[i][j]:
                bfs(i,j)
                result += 1
    print(result)

 

+ Recent posts