Problem Solving/BOJ & Programmers

[BOJ] 1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

geum 2024. 9. 26. 18:57

โœ๏ธ ๋ฌธ์ œ

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

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

 

1 1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 1 1 0 0 0 1 1 1
0 0 0 0 1 0 0 1 1 1

 

๐Ÿค– ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

๐Ÿ’ก ํ’€์ด

์˜ˆ์‹œ ๊ทธ๋ฆผ๊ณผ ๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž ๋ฐ”๋กœ BFS ๋ƒ„์ƒˆ๋ฅผ ๋งก์•˜๊ธฐ ๋•Œ๋ฌธ์— BFS๋กœ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ, DFS ํ’€์ด๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. BFS/DFS ๋ฌธ์ œ ํ’€ ๋•Œ ํ–‰, ์—ด ํ—ท๊ฐˆ๋ คํ•˜๋Š” ๊ฒŒ ์˜ˆ์ „๋ณด๋‹ค๋Š” ์ค„์–ด๋“  ๊ฒƒ ๊ฐ™๊ธด ํ•˜์ง€๋งŒ ๋” ์—ฐ์Šตํ•ด์•ผ๊ฒ ๋‹ค.

 

1) BFS ํ’€์ด

from collections import deque

def BFS(start_row, start_col, field, visited):
    queue = deque([(start_col, start_row)])
    
    visited[start_row][start_col] = True
    
    while queue:
        x, y = queue.popleft()
        
        dx = [0, 0, -1, 1]
        dy = [-1, 1, 0, 0]
        
        for i in range(4):
            new_x, new_y = x+dx[i], y+dy[i]
            
            if 0 <= new_x < len(field[0]) and 0 <= new_y < len(field) and field[new_y][new_x] == 1 and not visited[new_y][new_x]:
                    visited[new_y][new_x] = True
                    queue.append((new_x, new_y))

T = int(input())

for _ in range(T):
    M, N, K = map(int, input().split())

    farm = [[0]*M for _ in range(N)]
    visited = [[False]*M for _ in range(N)]
    
    worm = 0
    
    for _ in range(K):
        COL, ROW = map(int, input().split())    # ๋ฐฐ์ถ” ์œ„์น˜ X์—ด Yํ–‰
        
        farm[ROW][COL] = 1

    for row in range(N):
        for col in range(M):
            if farm[row][col] == 1 and not visited[row][col]:
                BFS(row, col, farm, visited)
                worm += 1
                
    print(worm)

 

2) DFS ํ’€์ด

๋ฐ”๋กœ ์œ„์—์„œ ํ–‰, ์—ด ํ—ท๊ฐˆ๋ คํ•˜๋Š” ๊ฒŒ ์ค€ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ํ•ด๋†“๊ณ  DFS๋กœ ํ’€๋‹ค๊ฐ€ ์ธ๋ฑ์Šค ์—๋Ÿฌ ๋งŒ๋‚˜์„œ ํ•œ์ฐธ ๋ถ™์žก๊ณ  ์žˆ์—ˆ๋‹ค ๋จธ์“ฑ~ ๊ทผ๋ฐ BFS ํ’€์ด๋ž‘ ๋˜‘๊ฐ™์ด ํ–‰, ์—ด ๋ณ€์ˆ˜ ์ผ๋Š”๋ฐ ์™œ ์ธ๋ฑ์Šค ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์„๊นŒ

 

import sys

sys.setrecursionlimit(10000)
    
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

dfs_count = 0

def DFS(start_row, start_col, field, visited, dfs_count):
    dfs_count += 1

    visited[start_row][start_col] = True
    
    for i in range(4):
        new_x, new_y = start_row+dx[i], start_col+dy[i]
        
        if 0 <= new_x < len(field) and 0 <= new_y < len(field[0]) and field[new_x][new_y] == 1 and not visited[new_x][new_y]:
            DFS(new_x, new_y, field, visited, dfs_count)
         
T = int(input())

for _ in range(T):
    M, N, K = map(int, input().split())

    farm = [[0]*M for _ in range(N)]
    visited = [[False]*M for _ in range(N)]
    
    worm = 0
    
    for _ in range(K):
        COL, ROW = map(int, input().split())    # ๋ฐฐ์ถ” ์œ„์น˜ X์—ด Yํ–‰
        
        farm[ROW][COL] = 1

    for row in range(N):
        for col in range(M):
            if farm[row][col] == 1 and not visited[row][col]:
                DFS(row, col, farm, visited, 0)
                worm += 1
                
    print(worm)

 

 

192ms๊ฐ€ DFS ํ’€์ด, 212ms๊ฐ€ BFS ํ’€์ด๋‹ค.