BOJ - 3184 - 양

Updated:

import sys
from collections import deque

R, C = map(int, sys.stdin.readline().split())
Field = []
for _ in range(R):
    Field.append(list(str(sys.stdin.readline()).strip()))
Check = [[False for _ in range(C)] for _ in range(R)]

def BFS(i, j, wolf, sheep):
    dx = [1, -1, 0, 0]
    dy = [0, 0, 1, -1]

    dq = deque()
    dq.append((i, j))
    Check[i][j] = True
    v, o = 0, 0

    while dq:
        x, y = dq.popleft()
        if Field[x][y] == 'v':
            v += 1
        if Field[x][y] == 'o':
            o += 1

        for step in range(4):
            nx, ny = x + dx[step], y + dy[step]
            if nx < 0 or nx >= R or ny < 0 or ny >= C:
               continue

            if not Check[nx][ny] and Field[nx][ny] != '#':

                Check[nx][ny] = True
                dq.append((nx, ny))

    if o > v: sheep[0] += o
    else: wolf[0] += v

def solution():
    wolf, sheep = [0], [0]
    for i in range(R):
        for j in range(C):
            if not Check[i][j] and Field[i][j] != "#":
                BFS(i, j, wolf, sheep)

    print(sheep[0], wolf[0])

solution()

https://www.acmicpc.net/problem/3184

Categories:

Updated:

Leave a comment