Skip to content

Commit d1cdc36

Browse files
committed
[22.02.23] 14716 너비 우선 탐색
1 parent 4191ade commit d1cdc36

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Diff for: Python_BOJ_2022/14716.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 현수막
2+
from collections import deque
3+
4+
m, n = map(int, input().split())
5+
graph = [list(map(int, input().split())) for _ in range(m)]
6+
visited = [[False] * n for _ in range(m)]
7+
dx, dy = [-1, 1, 0, 0, -1, 1, -1, 1], [0, 0, -1, 1, -1, -1, 1, 1]
8+
9+
def bfs(i, j):
10+
queue = deque()
11+
queue.append((i, j))
12+
while queue:
13+
x, y = queue.popleft()
14+
for i in range(8):
15+
nx, ny = x + dx[i], y + dy[i]
16+
if 0 <= nx < m and 0 <= ny < n:
17+
if not visited[nx][ny]:
18+
if graph[nx][ny] == 1:
19+
visited[nx][ny] = True
20+
queue.append((nx, ny))
21+
22+
ans = 0
23+
for i in range(m):
24+
for j in range(n):
25+
if graph[i][j] == 1 and not visited[i][j]:
26+
bfs(i, j)
27+
ans += 1
28+
29+
print(ans)

0 commit comments

Comments
 (0)