์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€ (boj) ํŒŒ์ด์ฌ - 2583๋ฒˆ: ์˜์—ญ ๊ตฌํ•˜๊ธฐ

  • -

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

 

2583๋ฒˆ: ์˜์—ญ ๊ตฌํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— M๊ณผ N, ๊ทธ๋ฆฌ๊ณ  K๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. M, N, K๋Š” ๋ชจ๋‘ 100 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ K๊ฐœ์˜ ์ค„์—๋Š” ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ง์‚ฌ๊ฐํ˜•์˜ ์™ผ์ชฝ ์•„๋ž˜ ๊ผญ์ง“์ ์˜ x, y์ขŒํ‘œ๊ฐ’๊ณผ ์˜ค

www.acmicpc.net

 

#๋ถ„๋ฅ˜ : DFS

 

#๋‚œ์ด๋„ : Silver 1

 

 

์ฝ”๋“œ(Python)

from sys import setrecursionlimit
setrecursionlimit(10000)

m,n,k = map(int, input().split())
map_ = [[0 for i in range(m)] for i in range(n)]

areas = []

def is_valid(x,y):
    if x >= 0 and y >= 0 and x < n and y < m :
        return True
    return False

def dfs(x,y):
    if not is_valid(x,y) :
        return False
    global cnt
    if map_[x][y] == 0 :
        map_[x][y] = 1
        dfs(x+1,y)
        dfs(x-1,y)
        dfs(x,y+1)
        dfs(x,y-1)
        cnt += 1
        return True
    return False


def draw_rectangle(s):
    s_x, s_y, e_x, e_y = s
    for x in range(s_x , e_x):
        for y in range(s_y , e_y):
            map_[x][y] = 1


for _ in range(k):
    s = list(map(int, input().split()))
    draw_rectangle(s)

for x in range(n):
    for y in range(m):
        cnt = 0
        if map_[x][y] == 0:
            dfs(x,y)
            areas.append(cnt)

print(len(areas))
print(*sorted(areas))
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.