[백준] 1926 : 그림

백지원·2023년 9월 6일
0
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
M = [list(map(int, input().split())) for _ in range(n)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]

from collections import deque
def bfs(i,j):
    q = deque([(i,j)])
    M[i][j]=0
    ans = 0
    while q:
        x, y = q.popleft()
        ans += 1
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if -1 < nx and nx < n and -1 < ny and ny < m and M[nx][ny]:
                M[nx][ny]=0
                q.append((nx,ny))
    return ans
                
pictures = [0]
for i in range(n):
    for j in range(m):
        if M[i][j]:
            pictures.append(bfs(i,j))

print(len(pictures)-1)
print(max(pictures))

왼쪽 위에서부터 하나씩 검토해가며 1인 부분을 확인.
1인 부분의 좌표를 bfs함수에 입력값으로 입력.
bfs 함수 : 입력된 좌표와 이어진 그림들을 0으로 바꾸어가며 그림의 크기를 return값으로 반환

0개의 댓글