BOJ - 3187 - 양치기 꿍

Updated:

import sys
from collections import deque

dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

def BFS(arr, check, x, y, R, C, ret):
    dq = deque()
    dq.append((x, y))
    check[x][y] = True

    wolf = 0
    sheep = 0

    while dq:
        x, y = dq.popleft()
        if arr[x][y] == 'v':
            wolf += 1
        elif arr[x][y] == 'k':
            sheep += 1

        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < R and 0 <= ny < C:
                if not check[nx][ny] and arr[nx][ny] != '#':
                    check[nx][ny] = True
                    dq.append((nx, ny))

    if sheep > wolf:
        ret[0] += sheep
    else:
        ret[1] += wolf

def solution():
    R, C = map(int, sys.stdin.readline().split())
    maps = []
    check = [[False for _ in range(C)] for _ in range(R)]
    for _ in range(R):
        maps.append(list(map(str, sys.stdin.readline().strip())))
    ret = [0, 0]
    for i in range(R):
        for j in range(C):
            if maps[i][j] != '#':
                if not check[i][j]:
                    BFS(maps, check, i, j, R, C, ret)

    print(ret[0], ret[1])

solution()

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

Categories:

Updated:

Leave a comment