반응형
코드
import sys
sys.setrecursionlimit(10000)
from collections import deque
def bfs(x,y):
global cnt
queue = deque()
queue.append((x,y))
while queue:
x,y = queue.popleft()
rec[x][y] = 1
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0<=nx<m and 0<=ny<n and rec[nx][ny]==0:
cnt+=1
bfs(nx,ny)
m,n,k = map(int,input().split())
rec = [[0]*n for _ in range(m)]
for _ in range(k):
x1,y1,x2,y2 = map(int,input().split())
for i in range(y1,y2):
for j in range(x1,x2):
rec[i][j] = 1
count = 0
cnt = 1
ans = []
dx = [-1,1,0,0]
dy = [0,0,-1,1]
for i in range(m):
for j in range(n):
if rec[i][j] == 0:
count += 1
bfs(i,j)
ans.append(cnt)
cnt = 1
print(count)
ans.sort()
print(*ans)
반응형
'알고리즘 > 그래프 탐색' 카테고리의 다른 글
[백준][Python] 1446번 지름길 (0) | 2022.12.30 |
---|---|
[백준][Python] 1743번 음식물 피하기 (0) | 2022.12.20 |
[백준][Python] 10026번 적록색약 (0) | 2022.11.29 |
[백준][Python] 14502번 연구소 (0) | 2022.11.09 |
[백준][Python] 1697번 숨바꼭질 (0) | 2022.10.26 |
댓글