[๋ฐฑ์ค€/python] 2667๋ฒˆ : ๋‹จ์ง€๋ฒˆํ˜ธ ๋ถ™์ด๊ธฐ

2021. 4. 7. 00:37

๋ฌธ์ œ

<๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ์ง€๋„๊ฐ€ ์žˆ๋‹ค. 1์€ ์ง‘์ด ์žˆ๋Š” ๊ณณ์„, 0์€ ์ง‘์ด ์—†๋Š” ๊ณณ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ฒ ์ˆ˜๋Š” ์ด ์ง€๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ๊ฒฐ๋œ ์ง‘์˜ ๋ชจ์ž„์ธ ๋‹จ์ง€๋ฅผ ์ •์˜ํ•˜๊ณ , ๋‹จ์ง€์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ด๋ ค ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์—ฐ๊ฒฐ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ค ์ง‘์ด ์ขŒ์šฐ, ํ˜น์€ ์•„๋ž˜์œ„๋กœ ๋‹ค๋ฅธ ์ง‘์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค. ๋Œ€๊ฐ์„ ์ƒ์— ์ง‘์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์—ฐ๊ฒฐ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. <๊ทธ๋ฆผ 2>๋Š” <๊ทธ๋ฆผ 1>์„ ๋‹จ์ง€๋ณ„๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ธ ๊ฒƒ์ด๋‹ค. ์ง€๋„๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๋‹จ์ง€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ฐ ๋‹จ์ง€์— ์†ํ•˜๋Š” ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ง€๋„์˜ ํฌ๊ธฐ N(์ •์‚ฌ๊ฐํ˜•์ด๋ฏ€๋กœ ๊ฐ€๋กœ์™€ ์„ธ๋กœ์˜ ํฌ๊ธฐ๋Š” ๊ฐ™์œผ๋ฉฐ 5≤N≤25)์ด ์ž…๋ ฅ๋˜๊ณ , ๊ทธ ๋‹ค์Œ N์ค„์—๋Š” ๊ฐ๊ฐ N๊ฐœ์˜ ์ž๋ฃŒ(0ํ˜น์€ 1)๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ด ๋‹จ์ง€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 

์ •๋‹ต

import sys
input = sys.stdin.readline

N = int(input())
houses = [list(map(int, list(input().strip()))) for _ in range(N)]
visited = [list(False for _ in range(N)) for _ in range(N)]
blocks = {}


def DFS(i, j, number):
    stack = [[i, j]]
    blocks[number] = 0
    while stack:
        n = stack.pop()
        if houses[n[0]][n[1]] == 1 and visited[n[0]][n[1]] == False:
            visited[n[0]][n[1]] = True
            blocks[number] += 1

            if n[0] - 1 >= 0:
                stack.append([n[0] - 1, n[1]])
            if n[0] + 1 < N:
                stack.append([n[0] + 1, n[1]])
            if n[1] - 1 >= 0:
                stack.append([n[0], n[1] - 1])
            if n[1] + 1 < N:
                stack.append([n[0], n[1] + 1])


n = 0
for i in range(N):
    for j in range(N):
        if houses[i][j] == 1 and visited[i][j] == False:
            DFS(i, j, n)
            n += 1

print(len(blocks))
blocks = sorted(blocks.items(), key=lambda x: x[1])
for i in blocks:
    print(i[1])

N = int(input())
houses = [list(map(int, list(input().strip()))) for _ in range(N)]
visited = [list(False for _ in range(N)) for _ in range(N)]
blocks = {}

์ง€๋„์˜ ํฌ๊ธฐ N๊ณผ ์ง‘์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ผ houses๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

๊ณต๋ฐฑ์ด ์—†๋Š” ์ž…๋ ฅ์€ strip()์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

ํ™•์ธํ•œ ๊ณณ์„ ํ‘œ์‹œํ•  visited๋ฅผ houses์™€ ๋˜‘๊ฐ™์€ ํฌ๊ธฐ๋กœ ๋งŒ๋“ ๋‹ค.

blocks์—๋Š” ๋ช‡ ๋‹จ์ง€์— ๋ช‡ ๊ฐ€๊ตฌ๊ฐ€ ์žˆ๋Š”์ง€ ์ €์žฅํ•  ๋”•์…”๋„ˆ๋ฆฌ์ด๋‹ค.

 

def DFS(i, j, number):
    stack = [[i, j]]
    blocks[number] = 0
    while stack:
        n = stack.pop()
        if houses[n[0]][n[1]] == 1 and visited[n[0]][n[1]] == False:
            visited[n[0]][n[1]] = True
            blocks[number] += 1

            if n[0] - 1 >= 0:
                stack.append([n[0] - 1, n[1]])
            if n[0] + 1 < N:
                stack.append([n[0] + 1, n[1]])
            if n[1] - 1 >= 0:
                stack.append([n[0], n[1] - 1])
            if n[1] + 1 < N:
                stack.append([n[0], n[1] + 1])

dfs๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒ€์‚ฌํ•œ๋‹ค.

i์™€ j๋Š” 2์ฐจ์›๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์ด๊ณ  number๋Š” ๋ช‡ ๋‹จ์ง€์ธ์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

number๋ฅผ blocks์˜ key๋กœ ์ €์žฅํ•˜๊ณ  value๋กœ ์ผ๋‹จ 0๊ฐ€๊ตฌ๋ฅผ ์˜๋ฏธํ•˜๋Š” 0์„ ๋„ฃ๋Š”๋‹ค.

 

n์—๋Š” ๊ฐ’์ด 2๊ฐœ์ธ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ €์žฅ๋˜๋ฉฐ 2์ฐจ์›๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค๋กœ ์“ฐ์ธ๋‹ค.

์ง‘์ด ์žˆ๊ณ (houses == 1) ๋ฐฉ๋ฌธํ•œ ์  ์—†์„ ๋•Œ(visited == False), 

visited๋ฅผ True๋กœ ๋ฐ”๊ฟ” ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ํ‘œ์‹œํ•˜๊ณ  blocks[number]์— 1์„ ๋”ํ•˜์—ฌ ๊ฐ€๊ตฌ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Œ์„ ํ‘œ์‹œํ•œ๋‹ค.

if ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ƒํ•˜์ขŒ์šฐ ๊ฐ’์ด ์ธ๋ฑ์Šค๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๋ฒ—์–ด๋‚˜์ง€ ์•Š์„ ๋•Œ stack์— ์ถ”๊ฐ€ํ•œ๋‹ค.

 

n = 0
for i in range(N):
    for j in range(N):
        if houses[i][j] == 1 and visited[i][j] == False:
            DFS(i, j, n)
            n += 1

houses์˜ ๋ชจ๋“  ์ธ๋ฑ์Šค๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ

ํ•ด๋‹น ์ธ๋ฑ์Šค๊ฐ€ 1์ด๊ณ  ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†์„ ๋•Œ DFS๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋‹จ์ง€ n์„ ๋งŒ๋“ ๋‹ค.

 

print(len(blocks))
blocks = sorted(blocks.items(), key=lambda x: x[1])
for i in blocks:
    print(i[1])

๊ฐ ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด sorted์™€ lambdaํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌํ•œ๋‹ค.

 

728x90

BELATED ARTICLES

more