Problem Solving/BOJ & Programmers

[BOJ] 2667๋ฒˆ: ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ

geum 2024. 9. 27. 15:13

โœ๏ธ ๋ฌธ์ œ

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

 

 

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

 

๐Ÿ’ก ํ’€์ด

BFS+DFS ๋ฌธ์ œ์ง‘ ๊ฐ€์ง€๊ณ  ๊ทธ๋ž˜ํ”„ ์œ ํ˜• ์—ด์‹ฌํžˆ ํ’€์–ด๋ณด๋Š” ์ค‘์ด๋‹ค. ๋ช‡ ๋ฒˆ์˜ ์ฝ”ํ…Œ๋ฅผ ๊ฒฝํ—˜ํ•ด๋ณธ ๊ฒฐ๊ณผ, ๋‚˜๋Š” ๊ธฐ๋ณธ ์œ ํ˜•์— ์กฐ๊ฑด์ด ํ•˜๋‚˜๋งŒ ์ถ”๊ฐ€๋ผ๋„ ์†์„ ๋ชป ๋Œ„๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฌธ์ œ์ฒ˜๋Ÿผ ์ถ”๊ฐ€์ ์ธ ์ฒ˜๋ฆฌ(๋‹จ์ง€ ๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ฒƒ)๋ฅผ ํ•ด์ค˜์•ผ ํ•˜๋Š” ์œ ํ˜•์„ ๋งŽ์ด ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.

1) DFS ํ’€์ด

import sys

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

board = [list(map(int, input())) for _ in range(N)]

visited = [[False]*N for _ in range(N)]

town = []

def DFS(row, col):
    global count
    
    count += 1

    visited[row][col] = True
    
    for i in range(4):
        new_row, new_col = row+dx[i], col+dy[i]
        
        if (0 <= new_row < len(board) and 0 <= new_col < len(board)) and visited[new_row][new_col] == 0 and board[new_row][new_col] == 1:
            DFS(new_row, new_col)

    return count

for row in range(N):
    for col in range(N):
        if board[row][col] == 1 and visited[row][col] == 0:
            count = 0
            
            town.append(DFS(row, col))

print(len(town))

for home in sorted(town):
    print(home)

 

2) BFS ํ’€์ด

from collections import deque

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
            
N = int(input())

board = [list(map(int, input())) for _ in range(N)]

visited = [[False]*N for _ in range(N)]

town = []

def BFS(row, col):
    global count

    queue = deque([(row, col)])
    
    visited[row][col] = True
    
    count += 1  # ์ž๊ธฐ ์ž์‹  ์ฒดํฌ
    
    while queue:
        curr_row, curr_col = queue.popleft()
        
        for i in range(4):
            new_row, new_col = curr_row+dx[i], curr_col+dy[i]
            
            if (0 <= new_row < len(board) and 0 <= new_col < len(board)) and visited[new_row][new_col] == 0 and board[new_row][new_col] == 1:
                count += 1
                
                visited[new_row][new_col] = True
                
                queue.append((new_row, new_col))
                
    return count

for row in range(N):
    for col in range(N):
        if board[row][col] == 1 and visited[row][col] == 0:
            count = 0
            
            town.append(BFS(row, col))

print(len(town))

for home in sorted(town):
    print(home)

 

BFS๋กœ ํ’€ ๋•Œ ์ž๊ธฐ ์ž์‹ ์„ ์ฒดํฌํ•˜๋Š” ๊ฑธ ๋นผ๋จน์–ด์„œ ๋‹จ์ง€ ๋‚ด ์ง‘์˜ ์ˆ˜๊ฐ€ ์ •๋‹ต๋ณด๋‹ค 1์”ฉ ์ž‘๊ฒŒ ๋‚˜์˜ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์ง€๋งŒ ํ•ด๊ฒฐ!

 

 

32ms๊ฐ€ DFS ํ’€์ด, 52ms๊ฐ€ BFS ํ’€์ด๋‹ค. ๊ทผ๋ฐ DFS/BFS ๊ณต๋ถ€ํ•  ๋•Œ BFS๊ฐ€ ๋” ๋น ๋ฅด๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ฒ˜๋ฆฌ ๋ฒ”์œ„๊ฐ€ ์ปค์ง€๋ฉด BFS์˜ ์†๋„ ์ฒด๊ฐ์ด ๋ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์•„์ง๊นŒ์ง€๋Š” DFS๋ฅผ ์‚ฌ์šฉํ•œ ํ’€์ด๊ฐ€ ๋” ๋น ๋ฅธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜๋‹ค.