728x90
반응형
BFS 문제이다. bfs탐색으로 각 울타리 영역의 양과, 늑대('o'와 'v')의 수를 센 후에,
각 울타리당 양의 수가 늑대보다 많으면 늑대를 0으로, 아니면 양을 0으로 바꿔주고 그 총합을 출력해주면 된다.
from collections import deque
def bfs(y, x):
queue = deque()
queue.append((y, x))
s = w = 0
while queue:
y, x = queue.popleft()
for dy, dx in d:
Y, X = y+dy, x+dx
if (0 <= Y < R) and (0 <= X < C) and graph[Y][X] != '#' and check[Y][X] == 0:
if graph[Y][X] == 'o':
s += 1
elif graph[Y][X] == 'v':
w += 1
check[Y][X] = 1
queue.append((Y, X))
return s, w
R, C = map(int, input().split())
graph = [list(input()) for _ in range(R)]
check = [[0]*(C) for _ in range(R)]
d = [(-1, 0), (1, 0), (0, -1), (0, 1)]
S = W = 0
for i in range(R):
for j in range(C):
if graph[i][j] != '#' and check[i][j] == 0:
s = w = 0
if graph[i][j] == 'o':
s += 1
elif graph[i][j] == 'v':
w += 1
check[i][j] = 1
a, b = bfs(i, j)
s, w = s+a, w+b
if s > w:
w = 0
else:
s = 0
S += s
W += w
print(S, W)
728x90
반응형
'Agorithm > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 6118번 숨바꼭질(python) (0) | 2021.03.12 |
---|---|
백준 알고리즘 16953번 A → B(python) (0) | 2021.03.12 |
백준 알고리즘 5567번 결혼식(python) (0) | 2021.03.11 |
백준 알고리즘 9205번 맥주 마시면서 걸어가기(python) (0) | 2021.03.11 |
백준 알고리즘 2589번 보물섬(python) (0) | 2021.03.11 |