본문 바로가기
알고리즘 문제 풀이/DFS,BFS

[백준]1926번: 그림

by syLim___ 2023. 7. 16.
728x90

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

 

1926번: 그림

어떤 큰 도화지에 그림이 그려져 있을 때, 그 그림의 개수와, 그 그림 중 넓이가 가장 넓은 것의 넓이를 출력하여라. 단, 그림이라는 것은 1로 연결된 것을 한 그림이라고 정의하자. 가로나 세로

www.acmicpc.net


bfs

import sys
from collections import deque
input = sys.stdin.readline

dx = [-1,0,1,0]
dy = [0,1,0,-1]


def bfs(x,y):
  q = deque()
  q.append((x,y))
  cnt = 0
  while q:
    x,y = q.popleft()
    for i in range(4):
      nx, ny = x + dx[i], y + dy[i]
      if 0 <= nx < n and 0 <= ny < m and paper[nx][ny] == 1:
        q.append((nx,ny))
        paper[nx][ny] = 2 # 방문표시
        cnt += 1
  return cnt if cnt>0 else 1
        
n, m = map(int, input().split())
paper = []
for _ in range(n):
  paper.append(list(map(int,input().split())))

count = 0
area = 0
for i in range(n):
  for j in range(m):
    if paper[i][j] == 1:
      count += 1
      area = max(area, bfs(i,j))
print(count)
print(area)

 

728x90