λ¬Έμ
λ°±μ€ μ¨λΌμΈ μ μ§ - 3184λ²
νμ΄ κ³Όμ
BFS
νμμ ν΅ν΄ λͺ¨λ μμμ νμν΄μ€ λ€ λλμ μμ μμ μλ₯Ό νμ
νλ λ¬Έμ μ
λλ€.
μ λ λλμ μμΉμμλΆν° BFS
νμμ μννμ§λ§
κ·Έλ₯ μμ§ λ°©λ¬Ένμ§ μμ λΉμΉΈμμλΆν° BFS
νμμ μμν΄λ μκ΄μμ κ² κ°μ΅λλ€.
μ½λ
import sys
from collections import deque
R, C = list(map(int, sys.stdin.readline().split()))
board = []
wolves = []
sheep = []
for r in range(R):
line = list(sys.stdin.readline()[:-1])
board.append(line)
for c in range(C):
if line[c] == 'v':
wolves.append([r, c])
if line[c] == 'o':
sheep.append([r, c])
visit = [[False] * C for _ in range(R)]
dx = [0 ,0, 1, -1]
dy = [1, -1, 0, 0]
def bfs(start):
w_c = 1
s_c = 0
q = deque()
q.append([start[0], start[1]])
visit[start[0]][start[1]] = True
while q:
x, y = q.popleft()
for i in range(4):
next_x, next_y = x + dx[i], y + dy[i]
if 0 <= next_x < R and 0 <= next_y < C:
if board[next_x][next_y] != '#' and not visit[next_x][next_y]:
visit[next_x][next_y] = True
q.append([next_x, next_y])
if board[next_x][next_y] == 'v':
w_c += 1
if board[next_x][next_y] == 'o':
s_c += 1
return w_c, s_c
def solution():
init_wolf = len(wolves)
init_sheep = len(sheep)
for wolf in wolves:
if not visit[wolf[0]][wolf[1]]:
w_c, s_c = bfs(wolf)
if w_c < s_c:
init_wolf -= w_c
else:
init_sheep -= s_c
print(init_sheep, init_wolf)
solution()
λ°μν
'π algorithm > boj' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ 2805 - λ무 μλ₯΄κΈ° (0) | 2021.03.18 |
---|---|
BOJ 1654 - λμ μλ₯΄κΈ° (0) | 2021.03.18 |
BOJ 7562 - λμ΄νΈμ μ΄λ (0) | 2021.03.18 |
BOJ 18352 - νΉμ 거리μ λμ μ°ΎκΈ° (0) | 2021.03.18 |
BOJ 16173 - μ νμ 쩰리(small) (0) | 2021.03.18 |
π¬ λκΈ