반응형
https://www.acmicpc.net/problem/2583
import sys
sys.setrecursionlimit(15000)
y_lim, x_lim, n = map(int,input().split())
graph = []
for i in range(x_lim):
graph.append([0]*y_lim)
for i in range(n):
x1, y1, x2, y2 = map(int,input().split())
for j in range(x1,x2):
for k in range(y1, y2):
graph[j][k]=1
dx=[-1,1,0,0]
dy=[0,0,-1,1]
def dfs(x,y):
graph[x][y]=1
sum = 1
for i in range(4):
nx = x+dx[i]
ny = y+dy[i]
if 0<=nx<x_lim and 0<=ny<y_lim and graph[nx][ny]==0:
sum += dfs(nx,ny)
return sum
count = 0
answer = []
for i in range(x_lim):
for j in range(y_lim):
if graph[i][j]==0:
answer.append(dfs(i,j))
count+=1
answer.sort()
print(count)
print(*answer)
기본적인 dfs 문제!
댓글