ν°μ€ν 리 λ·°
728x90
λ°μν
λ¬Έμ
λ¬Έμ νμ΄
ν μμμ μκ³Ό λλκ° μμ λ, μμ μκ° λλλ³΄λ€ λ§λ€λ©΄ μλ§ λ¨κ² λλ€. μ΄μ λ¬λ¦¬ μμ μκ° λλμ κ°κ±°λ λλλ³΄λ€ μ κ² λλ€λ©΄, μμ λͺ¨λ μ‘μλ¨Ήνκ³ λλλ§ λ¨κ² λλ€. μ΄λ, κ° μμμ μκ³Ό λλμ μλ₯Ό ꡬνμ¬ λ°ννλ λ¬Έμ μ΄λ€. λ¬Έμ λ μμ λ€λ£¬ λ¨μ§λ²νΈλΆμ΄κΈ°μ κ°μ λ‘μ§μΌλ‘ νλ, κ° μμμ μμ μμ λλμ μμ λ°λΌ 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
λ°μν
'π¨βπ» μ½λ©ν μ€νΈ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λ°±μ€: 17142 μ°κ΅¬μ 3 (0) | 2020.09.17 |
---|---|
λ°±μ€: 17141 μ°κ΅¬μ 2 (0) | 2020.09.17 |
λ°±μ€: 1600 λ§μ΄ λκ³ ν μμμ΄ (0) | 2020.09.14 |
λ°±μ€: 14395 4μ°μ° (0) | 2020.09.13 |
λ°±μ€: 10026 μ λ‘μμ½ (0) | 2020.09.13 |
λκΈ
κΈ λ³΄κ΄ν¨
μ΅κ·Όμ μ¬λΌμ¨ κΈ
μ΅κ·Όμ λ¬λ¦° λκΈ