https://www.acmicpc.net/problem/14716
from collections import deque
n,m =map(int, input().split())
arr =[]
dx =[1,-1,0,0,1,-1,1,-1]
dy =[0,0,1,-1,1,-1,-1,1]
ans = 0
for _ in range(n):
oneline = list(map(int, input().split()))
arr.append(oneline )
def bfs(i,j ) :
q = deque()
arr[i][j] = 2
q.append((i,j))
while q :
x,y = q.popleft()
for i in range(8):
nx,ny = x+dx[i] , y + dy[i]
if 0<=nx < n and 0<=ny < m :
if arr[nx][ny] == 1 :
q.append((nx,ny))
arr[nx][ny] = 2
for i in range(n):
for j in range(m):
if arr[i][j] ==1 :
bfs(i,j)
ans += 1
print(ans)
'알고리즘 문제풀이' 카테고리의 다른 글
거리가 k이하인 트리 노드에서 사과 수확하기[백준 25516] (0) | 2025.01.28 |
---|---|
영상처리[백준 21938] (0) | 2025.01.27 |
숨바꼭질[백준 6118] (0) | 2025.01.26 |
창영이와 점프[백준 22114] (1) | 2025.01.20 |
간식 파티 [백준 20162] (0) | 2025.01.20 |