BOJ - 1926 - 그림

Updated:

from collections import deque
import sys

def BFS(ret, Picture, Check, N, M, i, j):
    dx = [0, 0, 1, -1]
    dy = [1, -1, 0, 0]

    dq = deque()
    dq.append((i, j))
    size = 1
    Check[i][j] = True

    while dq:
        x, y = dq.popleft()

        for step in range(4):
            nx = x + dx[step]
            ny = y + dy[step]
            if nx < 0 or nx >= N or ny < 0 or ny >= M:
                continue
            if not Check[nx][ny] and Picture[nx][ny]:
                size += 1
                Check[nx][ny] = True
                dq.append((nx, ny))
    ret.append(size)

def solution():
    N, M = map(int, input().split(' '))
    Picture = []
    Check = [[False] * M for i in range(N)]
    ret = []
    for i in range(N):
        Picture.append(list(map(int, sys.stdin.readline().split())))

    for i in range(N):
        for j in range(M):
            if Picture[i][j] and not Check[i][j]:
                BFS(ret, Picture, Check, N, M, i, j)
    if ret:
        print(len(ret))
        print(max(ret))
    else:
        print(0)
        print(0)
solution()

https://www.acmicpc.net/problem/1926

Categories:

Updated:

Leave a comment