2583 : 영역구하기

서희찬·2021년 10월 6일
0

백준

목록 보기
59/105

문제

코드

import sys 
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
#m 세로 n가로 
m,n,k = map(int,input().split())
graph = [[0]*n for _ in range(m)]
cnt = 0

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

total =[]

def dfs(x,y):
    global wide #초기 넓이 
    graph[x][y]=1 #방문 ! 
    for i in range(4):
        cx = x+dx[i]
        cy = y+dy[i]
        if 0<=cx<m  and 0<=cy<n and graph[cx][cy]==0: #방문안한 곳 
            dfs(cx,cy)
            wide+=1 #넓이 올라가랏 
    

for _ in range(k): #사각형 색칠 
    x1,y1,x2,y2 = map(int,input().split())
    for j in range(y1,y2):
        for i in range(x1,x2):
            graph[j][i]=1 #색칠 

for j in range(m):
    for k in range(n):
        if graph[j][k]==0:
            dfs(j,k)
            total.append(wide)
            cnt+=1
            wide=1 #제일 첫 넓이는 1
    
total.sort()

print(cnt)
for totals in total:
    print(totals,end=" ")

해설

앞선 배추 문제에서 약간의 변형만 있는 문제이다.
행렬을 만들어준 후 사각형은 1로 저장한다.
그 후 0이 저장된 곳을 dfs탐색을 통해 탐색하는데, 매 사각형 넘어갈때마다 1씩 넓이를 올려주면되는데 제일 첫 사각형은 안올라가므로 wide=0이 아닌 1로 초기화시켜준다.

profile
부족한 실력을 엉덩이 힘으로 채워나가는 개발자 서희찬입니다 :)

0개의 댓글