[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ1] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2583 - ์˜์—ญ ๊ตฌํ•˜๊ธฐ

keyneneยท2022๋…„ 11์›” 3์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
18/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ1] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2583 - ์˜์—ญ ๊ตฌํ•˜๊ธฐ

๐Ÿ“œ๋ฌธ์ œ



DFS(๊นŠ์ด์šฐ์„ ํƒ์ƒ‰)

  • ์ž„์˜์˜ ํ•œ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ์žฌ๊ท€/์ˆœํ™˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ˜•ํƒœ์ž„
  • ๋…ธ๋“œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•ด๋†”์•ผ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์งˆ ์œ„ํ—˜์ด ์ค„์–ด๋“ฆ
  • ์ฃผ๋กœ ๋ฐฑํŠธ๋ž˜ํ‚น๊ณผ ํ•จ๊ป˜ ์“ฐ์—ฌ, ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋‹ค ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒƒ์„ ์ฐจ๋‹จํ•จ

๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

๋‚ด ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2606 - ๋ฐ”์ด๋Ÿฌ์Šค
*์ด ํฌ์ŠคํŒ…์„ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ •์„ DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด๋Š” ๋งˆ๋ฌด๋ฆฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค
(dy, dx๋ผ๋Š” ์ข‹์€ DFS์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด๋ฅผ ์ด์ œ์„œ์•ผ ์ดํ•ดํ–ˆ๊ธฐ ๋•Œ๋ฌธ...๐Ÿคฆ๐Ÿปโ€โ™€๏ธ)

์šฐ์„  ์ž…๋ ฅ๊ฐ’ m,n,k์™€ ์ฃผ์–ด์ง€๋Š” ๋…ธ๋“œ๋“ค์„ ์ €์žฅํ•˜์ž
๋…ธ๋“œ๋“ค์„ ์ด์šฉํ•ด for loof์œผ๋กœ ๋ชจ๋ˆˆ์ข…์ด๋ฅผ MAP์˜์—ญ์„ ์ƒ‰์น ํ•˜๊ณ ,
์™„์„ฑ๋œ MAP์„ ์ด์šฉํ•ด dfs()๋ฅผ ๋ฐ˜๋ณตํ˜ธ์ถœํ•˜๋ฉฐ
์ƒ‰์น ๋˜์ง€ ์•Š์€ ์˜์—ญ์˜ ๊ฐœ์ˆ˜ (cnt)์™€ ์˜์—ญ์˜ ๋„“์ด(dep)๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. m,n,k์™€ ์ฃผ์–ด์ง€๋Š” ๋…ธ๋“œ๋“ค์„ node(list)์— ์ €์žฅํ•˜๊ณ , amap(map)์„ n*mํฌ๊ธฐ๋กœ ์ดˆ๊ธฐํ™”ํ•˜์ž
  2. ๋…ธ๋“œ๋“ค์€ start point์™€ end point๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ, [[sx,sy,ex,ey], ...]ํ˜•ํƒœ๋กœ ์ €์žฅํ•ด๋‘๊ณ ,
    for loof๋ฅผ sxโ†’ex๊นŒ์ง€, syโ†’ey๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๋ชจ๋ˆˆ์ข…์ด๋ฅผ "1"๋กœ ์ƒ‰์น ํ•˜์ž
    : ๋˜ํ•œ, ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๊ทธ๋ฆผ๊ณผ ์‹ค์ œ ์ปดํ“จํ„ฐ์—์„œ ๊ทธ๋ ค์ง€๋Š” ๊ทธ๋ฆผ์€ ์ƒํ•˜๋ฐ˜์ „์ž„!
  3. for loof์„ ์ด์šฉํ•˜์—ฌ amap[y][x]์ด 0์ด๋ฉด dfs(x,y)๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ
    ํ•จ์ˆ˜์•ˆ์—์„œ ์ƒ‰์น ๋˜์ง€ ์•Š์€ ์˜์—ญ์˜ ๋„“์ด(dep)์„ ๊ณ„์‚ฐํ•˜์—ฌ, res์— ์ €์žฅํ•˜๊ณ ,
    ์ƒ‰์น ๋˜์ง€ ์•Š์€ ์˜์—ญ์˜ ๊ฐœ์ˆ˜(cnt)๋ฅผ +1 ์ฆ๊ฐ€์‹œํ‚ค์ž
    BUT
    ์„ฑ๊ณต์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜์—ฌ ์ œ์ถœํ•˜๋ฉด ๋Ÿฐํƒ€์ž„์—๋Ÿฌ ์ค‘ (Recursion Error)๋ฅผ ๋ณด๊ฒŒ๋˜๋Š”๋ฐ,
    ์ด๋Š” ๋ฐฑ์ค€์—์„œ ์žฌ๊ท€ํ˜ธ์ถœ์˜ ๊นŠ์ด๋ฅผ ์ž„์˜๋กœ "1000"์œผ๋กœ ์ง€์ •ํ•ด๋‘ฌ์„œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ์ด๋‹ค.
    
    ๐Ÿ‘‰๐Ÿปํ•ด๊ฒฐ๋ฐฉ๋ฒ•
     imprt sys
     sys.setrecursion(100000)
     sys ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์žฌ๊ท€ํ˜ธ์ถœ์˜ ๊นŠ์ด๋ฅผ ์ž„์‹œํฌ๊ธฐ๋กœ ๋Š˜๋ฆฌ์ž!
     100000์ด๋“ , 10000000์ด๋“  ๋‚ด ํ•จ์ˆ˜์˜ ์ตœ๋Œ€๊นŠ์ด๋ณด๋‹ค ๊นŠ์œผ๋ฉด ๋œ๋‹ค.
     
     ๋˜๋Š”
     dx, dy๋ฅผ ์ด์šฉํ•œ DFS์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š”๋ฐ ์ด๋Š” ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ๋‹ค.

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)

m,n,k = map(int, input().split())
node = [list(map(int,input().split())) for _ in range(k)]
amap = [[0 for _ in range(n)] for _ in range(m)]
cnt = 0   #์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ตœ์ข…๋ฐ˜ํ™˜ํ•  ๋ณ€์ˆ˜
res = []  #์˜์—ญ ๋‹น ๋„“์ด๋ฅผ ์ตœ์ข…๋ฐ˜ํ™˜ํ•  list (sort๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœํ•  ์˜ˆ์ •)

#๋ชจ๋ˆˆ์ข…์ด ์ƒ‰์น ํ•˜๊ธฐ
for sx,sy,ex,ey in node:
    for x in range(sx,ex):
        for y in range(sy,ey):
            amap[y][x] = 1

#์ƒ‰์น ์•ˆ๋œ ์˜์—ญ์˜ ๋„“์ด ๊ตฌํ•˜๋ฉด์„œ ์ƒ‰์น ํ•ด์ฃผ๊ธฐ (์žฌ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๊ฒŒ ํ•˜๋ ค๊ณ  ์ƒ‰์น )
def dfs(x,y):
    global dep
    if x<0 or y<0 or x>=n or y>=m:
        return
    if amap[y][x]:  
        return
    else:  
        dep += 1   #ํ˜„์žฌ ์˜์—ญ์˜ ๋„“์ด๋ฅผ ์ €์žฅ(global์ด๋ฏ€๋กœ ํ•จ์ˆ˜ ๋ฐ–์œผ๋กœ ์ „๋‹ฌ ๊ฐ€๋Šฅ) 
        amap[y][x] = 1  
        dfs(x-1,y)
        dfs(x,y-1)
        dfs(x+1,y)
        dfs(x,y+1)
    return

#์ƒ‰์น ์•ˆ๋œ ์˜์—ญ ๋„“์ด, ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
for y in range(m):
    for x in range(n):
        if not amap[y][x]:
            dep = 0
            dfs(x,y)
            res.append(dep)
            cnt += 1  #์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•จ
print(cnt)
print(*sorted(res))

โœ๏ธ1. n, m, node ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ amap ์ดˆ๊ธฐํ™”

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)

m,n,k = map(int, input().split())
node = [list(map(int,input().split())) for _ in range(k)]
amap = [[0 for _ in range(n)] for _ in range(m)]
cnt = 0   #์ƒ‰์น ์•ˆ๋œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ตœ์ข…๋ฐ˜ํ™˜ํ•  ๋ณ€์ˆ˜
res = []  #์ƒ‰์น ์•ˆ๋œ ์˜์—ญ ๋‹น ๋„“์ด๋ฅผ ์ตœ์ข…๋ฐ˜ํ™˜ํ•  ๋ณ€์ˆ˜ (์˜ค๋ฆ„์ฐจ์ˆœ ํ•  ์˜ˆ์ •)

โœ๏ธ2. node(list)๋ฅผ ์ด์šฉํ•˜์—ฌ amap ๋ชจ๋ˆˆ์ข…์ด ์ƒ‰์น ํ•˜๊ธฐ

#๋ชจ๋ˆˆ์ข…์ด ์ƒ‰์น ํ•˜๊ธฐ
for sx,sy,ex,ey in node:
    for x in range(sx,ex):
        for y in range(sy,ey):
            amap[y][x] = 1
โ“๋™์ž‘์›๋ฆฌ

1.๋ฒˆ์—์„œ ์ด๋ฏธ node์— [[sx,sy,ex,ey] ...]ํ˜•ํƒœ๋กœ ์ €์žฅํ•ด๋’€๋‹ค
๊ทธ node๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด๋ฉด์„œ x๊ฐ’์€ sxโ†’ex๋งŒํผ, y๊ฐ’์€ syโ†’ey๋งŒํผ ์ƒ‰์น ํ•œ๋‹ค.

โ€ปex, ey๊ฐ’์€ ์ด๋ฏธ 1์”ฉ ๋”ํ•ด์ ธ ์žˆ์œผ๋ฏ€๋กœ -1ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค.
  (sx=2, ex=4์ด๋ฉด 2,3์ด ์ƒ‰์น ๋œ๋‹ค)

โœ๏ธ3. dfs(x,y)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ‰์น  ์•ˆ ๋œ ์˜์—ญ ํฌ๊ธฐ/๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

#์ƒ‰์น ์•ˆ๋œ ์˜์—ญ์˜ ๋„“์ด ๊ตฌํ•˜๋ฉด์„œ ์ƒ‰์น ํ•ด์ฃผ๊ธฐ (์žฌ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๊ฒŒ ํ•˜๋ ค๊ณ  ์ƒ‰์น )
def dfs(x,y):
    global dep
    if x<0 or y<0 or x>=n or y>=m:  #์ฃผ์–ด์ง„ ์˜์—ญ ๋ฒ—์–ด๋‚˜๋ฉด return
        return
    if amap[y][x]:  #ํ˜„์žฌ amap์ด 1, ์ฆ‰ ์ƒ‰์น ๋˜์–ด ์žˆ์œผ๋ฉด return
        return      #(์ƒ‰์น  ์•ˆ ๋œ ์˜์—ญ์„ ๊ตฌํ• ๊บผ๋‹ˆ๊นŒ)
    else:           #ํ˜„์žฌ amap์ด 0, ์ƒ‰์น ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด
        dep += 1    #๋„“์ด ์ฆ๊ฐ€
        amap[y][x] = 1  #ํ˜„ amap์žฌ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋„๋ก 1๋กœ ์ƒ‰์น 
        dfs(x-1,y)     #4๋ฐฉํ–ฅ์œผ๋กœ dfs ์žฌํ˜ธ์ถœ
        dfs(x,y-1)
        dfs(x+1,y)
        dfs(x,y+1)
    return
    
#์ƒ‰์น ์•ˆ๋œ ์˜์—ญ ๋„“์ด, ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
for y in range(m):
    for x in range(n):
        if not amap[y][x]:  #amap์ด ํ˜„์žฌ ์ƒ‰์น ์ด ์•ˆ ๋˜์–ด ์žˆ์œผ๋ฉด
            dep = 0
            dfs(x,y)	    #dfs()์— x,y๊ฐ’ ์ „๋‹ฌ
            res.append(dep) #dfs()์—์„œ ์ €์žฅํ•œ dep๊ฐ’ res์— ์ €์žฅ
            cnt += 1        #dfs()๊ฐ€ ํ•œ ๋ฒˆ ๋™์ž‘ํ–ˆ์œผ๋ฏ€๋กœ cnt+1
print(cnt)
print(*sorted(res))   #print์˜ "*"์š”์†Œ๋Š” list์˜ "[]"๋ฅผ ๋–ผ๊ณ  ์ถœ๋ ฅํ•ด์ค€๋‹ค

๐Ÿ“š์ •๋ฆฌ

๋‚ด ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2606 - ๋ฐ”์ด๋Ÿฌ์Šค
๋‚ด ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 11724 - ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜
ํ˜„์žฌ ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ1] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2583 - ์˜์—ญ ๊ตฌํ•˜๊ธฐ
์™ธ,
๋ฐฑ์ค€ DFS๋ฌธ์ œ ๋ชจ์Œ ์—์„œ "์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜"๋ถ€ํ„ฐ ํ˜„ ๋ฌธ์ œ "์˜์—ญ๊ตฌํ•˜๊ธฐ" ๊นŒ์ง€
์ •์„ DFS์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€์–ด๋ณด์•˜๋‹ค.
(dfs(x,y)๋ฅผ 4๋ฐฉํ–ฅ(x-1,y), (x+1,y), (x,y-1), (x,y+1)์œผ๋กœ ์žฌ๊ท€ํ˜ธ์ถœํ•˜๋ฉด์„œ ํ‘ธ๋Š” ๋ฐฉ์‹)

์ด์ œ ์ •์„ DFS์•Œ๊ณ ๋ฆฌ์ฆ˜๋ฐฉ์‹์€ ์ถฉ๋ถ„ํžˆ ์—ฐ์Šตํ•œ ๊ฒƒ ๊ฐ™์œผ๋‹ˆ,
dx[-1,1,0,0], dy[0,0,-1,1]๊ณผ ๊ฐ™์ด ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•œ DFS์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด๋ฅผ ์—ฐ์Šตํ•˜๋ ค ํ•œ๋‹ค.

๊ทธ๋ ‡๋‹ค๊ณ  dx,dy๋ฅผ ์ด์šฉํ•œ DFS๊ฐ€ ์ •์„์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฑด ์•„๋‹˜....

profile
keynene
post-custom-banner

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