본문 바로가기
알고리즘/백준

백준-1926번(그림)-python3

by nyeongha 2023. 11. 24.
from collections import deque


# 도화지의 세로 크기 n(1 ≤ n ≤ 500)과 가로 크기 m(1 ≤ m ≤ 500)
n,m=map(int,input().split())
art_num=0

one_num=[0]

visit=[[0 for i in range(m)] for j in range(n)]
canvas=[]


for _ in range(n):
    canvas.append(list(map(int,input().split())))



def bfs(a,b):
    on=0
    q=deque([(a,b)])
    
    while q:
        x,y=q.popleft()
        visit[x][y]=1
        for sero,garo in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
            if 0<=sero<n and 0<=garo<m and canvas[sero][garo]==1 and not visit[sero][garo]:
                visit[sero][garo]=1
                on+=1
                q.append((sero,garo))
                
    return on

for i in range(n):
    for j in range(m):
        if canvas[i][j]==1 and not visit[i][j]:
            one_num.append(bfs(i,j)+1)
            art_num+=1

print(art_num)
print(max(one_num))

'알고리즘 > 백준' 카테고리의 다른 글

백준-1929번(소수 구하기)-python3  (0) 2023.11.24
백준-1927번(최소 힙)-python3  (0) 2023.11.24
백준-1920번(수 찾기)-python3  (0) 2023.11.24
백준-1912번(연속합)-python3  (0) 2023.11.24
백준-1904번(01타일)-python3  (0) 2023.11.24