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
'알고리즘 문제 풀이 > DFS,BFS' 카테고리의 다른 글
[백준]5014번: 스타트링크 (0) | 2023.07.23 |
---|---|
[백준]11725번: 트리의 부모 찾기 (0) | 2023.07.18 |
[백준]14940번: 쉬운 최단거리 (0) | 2023.07.03 |
[프로그래머스]미로탈출 (0) | 2023.06.23 |
[softeer] 장애물 인식 프로그램 (0) | 2023.05.16 |