Skip to content

Commit d604f40

Browse files
committed
[2023-12-12] BOJ 3187 풀이
1 parent fb3b2bf commit d604f40

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

BOJ/graph_traversal/BOJ_3187.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from collections import deque
2+
3+
def bfs(i, j):
4+
visited[i][j] = True
5+
s, w = 0, 0
6+
que = deque([(i, j)])
7+
while que:
8+
x, y = que.popleft()
9+
if array[x][y] == 'k':
10+
s += 1
11+
elif array[x][y] == 'v':
12+
w += 1
13+
for k in range(4):
14+
nx = x + dx[k]
15+
ny = y + dy[k]
16+
if 0 <= nx < r and 0 <= ny < c:
17+
if not visited[nx][ny] and array[nx][ny] != '#':
18+
que.append((nx, ny))
19+
visited[nx][ny] = True
20+
if s > w:
21+
return [s, 0]
22+
else:
23+
return [0, w]
24+
25+
r, c = map(int, input().split())
26+
array = [list(input()) for _ in range(r)]
27+
visited = [[False] * c for _ in range(r)]
28+
dx = [0, 0, 1, -1]
29+
dy = [1, -1, 0, 0]
30+
sheep, wolf = 0, 0
31+
32+
for i in range(r):
33+
for j in range(c):
34+
if array[i][j] in ['v', 'k'] and not visited[i][j]:
35+
result = bfs(i, j)
36+
sheep += result[0]
37+
wolf += result[1]
38+
print(sheep, wolf)

0 commit comments

Comments
 (0)