ν‹°μŠ€ν† λ¦¬ λ·°

728x90
λ°˜μ‘ν˜•

문제

 

3184번: μ–‘

첫 μ€„μ—λŠ” 두 μ •μˆ˜ Rκ³Ό Cκ°€ 주어지며(3 ≤ R, C ≤ 250), 각 μˆ˜λŠ” λ§ˆλ‹Ήμ˜ ν–‰κ³Ό μ—΄μ˜ 수λ₯Ό μ˜λ―Έν•œλ‹€. λ‹€μŒ R개의 쀄은 C개의 κΈ€μžλ₯Ό 가진닀. 이듀은 λ§ˆλ‹Ήμ˜ ꡬ쑰(μšΈνƒ€λ¦¬, μ–‘, λŠ‘λŒ€μ˜ μœ„μΉ˜)λ₯Ό μ˜λ―Έν•œλ‹€.

www.acmicpc.net

 

문제 풀이

 ν•œ μ˜μ—­μ— μ–‘κ³Ό λŠ‘λŒ€κ°€ μžˆμ„ λ•Œ, μ–‘μ˜ μˆ˜κ°€ λŠ‘λŒ€λ³΄λ‹€ λ§Žλ‹€λ©΄ μ–‘λ§Œ λ‚¨κ²Œ λœλ‹€. 이와 달리 μ–‘μ˜ μˆ˜κ°€ λŠ‘λŒ€μ™€ κ°™κ±°λ‚˜ λŠ‘λŒ€λ³΄λ‹€ 적게 λœλ‹€λ©΄, 양은 λͺ¨λ‘ μž‘μ•„λ¨Ήνžˆκ³  λŠ‘λŒ€λ§Œ λ‚¨κ²Œ λœλ‹€. μ΄λ•Œ, 각 μ˜μ—­μ— μ–‘κ³Ό λŠ‘λŒ€μ˜ 수λ₯Ό κ΅¬ν•˜μ—¬ λ°˜ν™˜ν•˜λŠ” λ¬Έμ œμ΄λ‹€. λ¬Έμ œλŠ” μ•žμ„œ 닀룬 λ‹¨μ§€λ²ˆν˜ΈλΆ™μ΄κΈ°μ™€ 같은 둜직으둜 ν’€λ˜, 각 μ˜μ—­μ˜ μ–‘μ˜ μˆ˜μ™€ λŠ‘λŒ€μ˜ μˆ˜μ— 따라 BFS 호좜 후에 λ°˜ν™˜λ˜λŠ” 값을 λ‹¬λ¦¬ν•˜λ©΄ λœλ‹€.

 

μ½”λ“œ

from sys import stdin
from collections import deque


def visitable(x, y):
    return 0 <= x < r and 0 <= y < c and not visited[x][y] and graph[x][y] != '#'


def bfs(start):
    q = deque([start])
    dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
    wolf, sheep = 0, 0

    while q:
        x, y = q.popleft()
        if graph[x][y] == 'o':
            sheep += 1
        elif graph[x][y] == 'v':
            wolf += 1
        for dx, dy in dirs:
            next_x, next_y = x + dx, y + dy
            if visitable(next_x, next_y):
                visited[next_x][next_y] = True
                q.append((next_x, next_y))
    return [0, sheep] if sheep > wolf else [1, wolf]


if __name__ == '__main__':
    r, c = map(int, stdin.readline().split())
    graph = [list(stdin.readline().strip()) for _ in range(r)]
    visited = [[False] * c for _ in range(r)]
    answer = [0, 0]

    for i in range(r):
        for j in range(c):
            if visitable(i, j):
                visited[i][j] = True
                idx, ret = bfs([i, j])
                answer[idx] += ret

    print(*answer)

 

728x90
λ°˜μ‘ν˜•
λŒ“κΈ€
κΈ€ 보관함
μ΅œκ·Όμ— 올라온 κΈ€
μ΅œκ·Όμ— 달린 λŒ“κΈ€