๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€ - 2023.12.28 ์˜ค๋Š˜์˜ ๋ฐฑ์ค€ ๋ฌธ์ œํ’€์ด

์œ ๋ น๊ฐœยท2023๋…„ 12์›” 28์ผ
0

PS-์•Œ๊ณ ๋ฆฌ์ฆ˜2

๋ชฉ๋ก ๋ณด๊ธฐ
4/34
post-thumbnail

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

๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋„ํ™”์ง€ ์œ„์— ์ง์‚ฌ๊ฐํ˜•์˜ ์ง‘ํ•ฉ์ด ์ฃผ์–ด์ง€๋ฉด ์ง์‚ฌ๊ฐํ˜•์„ ์ œ์™ธํ•œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜์™€ ๋„“์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

que = deque()
amount = []
M,N,K = map(int,input().split())
graph = [[0]*N for _ in range(M)]
for _ in range(K):
    a,b,c,d = map(int,input().split())
    for i in range(b,d):
        for j in range(a,c):
            graph[i][j] = 1

BFS๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด que๋ฅผ ๋ฐ›์•„์˜ค๊ณ  ๊ทธ๋ž˜ํ”„๋ฅผ ์ด์ฐจ์› ๋ฆฌ์ŠคํŠธ ์œ„์— ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด a,b,c,d ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์€ ๋‹ค์Œ for๋ฌธ์„ ํ†ตํ•ด ์ง์‚ฌ๊ฐํ˜•์ด ๊ทธ๋ ค์ง„ ์œ„ ๋„ํ™”์ง€๋ฅผ ๊ตฌํ˜„ํ•ด์ค์‹œ๋‹ค.

์ถœ๋ ฅ ์ž์ฒด๋Š” ์œ„ ๋„ํ™”์ง€ ์ผ€์ด์Šค๋ž‘ ์ƒํ•˜์ขŒ์šฐ ๋ฐ˜์ „์„ ์‹œํ‚จ ๊ฐ’์œผ๋กœ ๋‚˜์˜ค๋Š”๋ฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ฐ’์ด๋‚˜ ์ฃผ์–ด์ง„ ๊ณต๊ฐ„์˜ ๋„“์ด ์ง์‚ฌ๊ฐํ˜•์˜ ๋„“์ด ๋“ฑ ๊ตฌํ•˜๋Š” ๋ฐ๋Š” ์ง€์žฅ์ด ์—†์œผ๋‹ˆ๊นŒ ์ง„ํ–‰ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

sum = 0
for m in range(M):
    for n in range(N):
        if graph[m][n] == 0:
            graph[m][n] = 2
            amount.append(bfs(m,n)+1)
            sum += 1

๋„ํ™”์ง€ ์œ„์— ์ง์‚ฌ๊ฐํ˜•์ด ์•„๋‹Œ ๋นˆ๊ณต๊ฐ„์€ 2๋กœ ํ‘œ๊ธฐํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํƒ์ƒ‰์ด ์•ˆ๋œ ๋นˆ๊ณต๊ฐ„์˜ ์ดˆ๊ธฐ๊ฐ’์€ 0์ด๋ฏ€๋กœ 0์ผ๋•Œ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜๋Š” ์ž๊ธฐ์ž์‹ ์„ 2๋กœ ์„ค์ •ํ•˜๊ณ  BFS ํ•จ์ˆ˜๋ฅผ ๋Œ๋ฆฝ๋‹ˆ๋‹ค.

from collections import deque
dx = [-1,1,0,0]
dy = [0,0,1,-1]

def bfs(x,y):
    a = 0
    que.append([x,y])
    while que:
        x,y = que.popleft()
        for i in range(4):
            nx = dx[i] + x
            ny = dy[i] + y
            if nx<0 or ny<0 or nx>=M or ny>=N:
                continue 
            if graph[nx][ny] == 0:
                graph[nx][ny] = 2
                a += 1
                que.append([nx,ny])
    return a

ํ•ญ์ƒ ํ•˜๋˜ ๊ฒƒ์ฒ˜๋Ÿผ 4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ์‹ค์‹œํ•˜๋Š” ์ค‘์— ๋งŒ์•ฝ 4๋ฐฉํ–ฅ ์ค‘์—์„œ ํƒ์ƒ‰์ด ์•ˆ๋œ ๋นˆ๊ณต๊ฐ„ 0์„ ๋งŒ๋‚˜๋ฉด 2๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ๋นˆ๊ณต๊ฐ„์˜ ๋„“์ด๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ๊ณต๊ฐ„์„ ๋งŒ๋‚ ๋•Œ๋งˆ๋‹ค a๊ฐ’์— 1์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ๋Š” ๋นˆ๊ณต๊ฐ„์˜ ๋„“์ด๋ฅผ ๋ฆฌํ„ดํ•ด์ค๋‹ˆ๋‹ค.

            amount.append(bfs(m,n)+1)
            sum += 1

์•„๊นŒ ์œ„์— ํƒ์ƒ‰์‹œ์ž‘ for๋ฌธ ์•ˆ์— ์ด๋Ÿฐ๊ฒŒ ์ ํ˜€์žˆ์—ˆ๋Š”๋ฐ ๋„“์ด๋ฅผ ๊ตฌํ•˜๋Š” ์‹์€ ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธ์‹œํ‚ค๊ณ  ํƒ์ƒ‰์„ ํ•˜๋ฏ€๋กœ 1์„ ์ถ”๊ฐ€์‹œํ‚จ ๋„“์ด๊ฐ’์„ amount ๋ฐฐ์—ด์— ์ถ”๊ฐ€์‹œํ‚ค๊ณ  bfs๊ฐ€ ๋๋‚ฌ์œผ๋ฉด ์ตœ์ข…์ ์œผ๋กœ ๊ทธ ๊ตฌ์—ญ ํƒ์ƒ‰์€ ์ข…๋ฃŒ๋œ ๊ฒƒ์ด๋ฏ€๋กœ ์ง์‚ฌ๊ฐํ˜•์ด ์•„๋‹Œ ๊ณต๊ฐ„์˜ ๊ฐœ์ˆ˜ + 1์„ ํ•ด์ค๋‹ˆ๋‹ค.

amount.sort()
print(sum)
for values in amount:
    print(values,end=' ')

๋งˆ์ง€๋ง‰์œผ๋กœ amount๋ฅผ ์ •๋ ฌํ•˜๊ณ  sum๊ฐ’๊ณผ amount ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋‹ต์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.


profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€