취준시절/백준

[백준 3187] 양치기 꿍 - Python

MAYMIN 2021. 7. 5. 13:19
728x90
SMALL
#같은 울타리 영역 안의 양들의 숫자가 늑대의 숫자보다 더 많을 경우 늑대가 모두 잡아먹힘
#그외의 경우는 양이 다잡아먹힌다.
# 빈 공간은 . ,올타리는 # , 늑대는 v , 양은 k
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

r, c = map(int,input().split())
direction = [(1,0),(-1,0),(0,1),(0,-1)]
visited = [[0]* c for _ in range(r)]


def dfs(x,y):
    global wolf,sheep
    visited[x][y]=1

    if graph[x][y]=='v':
        wolf+=1
    if graph[x][y]=='k':
        sheep+=1
    for i in range(len(direction)):
        dx = x + direction[i][0]
        dy = y + direction[i][1]

        if 0<=dx<r and 0<=dy<c and visited[dx][dy]==0 and graph[dx][dy]!='#':
            dfs(dx,dy)


#graph =[[] * (r+1) for _ in range(c+1)]
# sheep = []
# wolf = []
graph=[]
for _ in range(r):
    graph.append(list(input().rstrip()))

wolfNum=0
sheepNum=0
for i in range(r):
    for j in range(c):
        if graph[i][j]!='#' and visited[i][j]==0:
            wolf = 0
            sheep = 0
            dfs(i,j)
            if wolf>=sheep:
                sheep=0
            else:
                wolf=0
            wolfNum+=wolf
            sheepNum+=sheep

print(sheepNum,wolfNum)
728x90
LIST