2ํ•™๋…„ 2ํ•™๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณผ์ œ_2์ฃผ์ฐจ(๋ฐฑ์ค€_1012๋ฒˆ:์œ ๊ธฐ๋† ๋ฐฐ์ถ”)

2024. 9. 30. 08:54ใ†Algorithm ( p & swlug )/Baekjoon

๐Ÿ“Œ๊ณผ์ œ2

1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ” (acmicpc.net)

 

 

 

 

๋ฌธ์ œ

 

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

ํ•œ๋‚˜๊ฐ€ ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋Š” ๋•…์€ ๊ณ ๋ฅด์ง€ ๋ชปํ•ด์„œ ๋ฐฐ์ถ”๋ฅผ ๊ตฐ๋ฐ๊ตฐ๋ฐ ์‹ฌ์–ด ๋†“์•˜๋‹ค. ๋ฐฐ์ถ”๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ๊ณณ์—๋Š” ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•œ ๋งˆ๋ฆฌ๋งŒ ์žˆ์œผ๋ฉด ๋˜๋ฏ€๋กœ ์„œ๋กœ ์ธ์ ‘ํ•ด์žˆ๋Š” ๋ฐฐ์ถ”๋“ค์ด ๋ช‡ ๊ตฐ๋ฐ์— ํผ์ ธ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•˜๋ฉด ์ด ๋ช‡ ๋งˆ๋ฆฌ์˜ ์ง€๋ ์ด๊ฐ€ ํ•„์š”ํ•œ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์ถ”๋ฐญ์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด ์ตœ์†Œ 5๋งˆ๋ฆฌ์˜ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•„์š”ํ•˜๋‹ค. 0์€ ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ์ง€ ์•Š์€ ๋•…์ด๊ณ , 1์€ ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ๋Š” ๋•…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

์ž…๋ ฅ

์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ์ฒซ์งธ ์ค„์—๋Š” ๋ฐฐ์ถ”๋ฅผ ์‹ฌ์€ ๋ฐฐ์ถ”๋ฐญ์˜ ๊ฐ€๋กœ๊ธธ์ด M(1 ≤ M ≤ 50)๊ณผ ์„ธ๋กœ๊ธธ์ด N(1 ≤ N ≤ 50), ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ๋Š” ์œ„์น˜์˜ ๊ฐœ์ˆ˜ K(1 ≤ K ≤ 2500)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ K์ค„์—๋Š” ๋ฐฐ์ถ”์˜ ์œ„์น˜ X(0 ≤ X ≤ M-1), Y(0 ≤ Y ≤ N-1)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฐฐ์ถ”์˜ ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ์˜ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด ๋งˆ๋ฆฌ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 

 ๐Ÿ“Œ๋‚ด ํ’€์ด

 

 

์šฐ์„  ํ•œ ๋ฐฐ์ถ”๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ๋ฐฉ์œผ๋กœ ๋ฐฐ์ถ”๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ, ๊ณ„์† ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด์˜ ์ˆ˜ํ˜œ๋ฅผ ๋ˆ„๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.

๊ทผ๋ฐ ์ด๊ฑธ ๋ฌธ์ œ๋กœ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์ขŒํ‘œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์•ผํ•  ๊ฒƒ ๊ฐ™์€๋ฐ, ์ผ๋‹จ์€ for๋ฌธ์„ ํ™œ์šฉํ•ด์„œ ์ƒํ•˜์ขŒ์šฐ ์ขŒํ‘œ์ด๋™ํ•˜๋“ฏ์ด

ํ’€์–ด๋‚ด๋ฉด ๋  ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

์ž‘๋…„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์—…์‹œ๊ฐ„์— ๋ฐฐ์› ๋˜ ๊ธฐ์–ต์„ ๋‹ค๋“ฌ์–ด๊ฐ€๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๋Š”๋ฐ for๋ฌธ๊ณผ ์ขŒํ‘œ x,y๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ ์ด์ƒ์œผ๋กœ ๋‚˜์•„๊ฐ€์ง€ ๋ชปํ–ˆ๋‹ค. 

๊ทธ๋ž˜์„œ ํ•ด๋‹น ๊ด€๋ จ๋œ ๋‚ด์šฉ์„ ์ฐพ์•„๋ณด์•˜๋‹ค.

 

๐Ÿ“Œ  ์ฝ”๋“œ & ์ฝ”๋“œ ๋ถ„์„

 

import sys
sys.setrecursionlimit(10000)

def dfs(x, y):
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]
    
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if (0 <= nx < m) and (0 <= ny < n):
            if field[ny][nx] == 1:
                field[ny][nx] = 0
                dfs(nx, ny)
                
for _ in range(int(sys.stdin.readline())):
    m, n, k = map(int, sys.stdin.readline().split())
    field = [[0 for _ in range(m)] for _ in range(n)]
    count = 0
    for _ in range(k):
        x, y = map(int, sys.stdin.readline().split())
        field[y][x] = 1
    for x in range(m):
        for y in range(n):
            if field[y][x] == 1:
                dfs(x, y)
                count += 1
    print(count)

 

 

ํ’€์ด ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•ด๋ณด๊ฒ ๋‹ค.

 

 

์ผ๋‹จ ์ฒซ์ค„์„ ์‚ดํŽด๋ณด์ž.

import sys
sys.setrecursionlimit(10000)

 

 

 ํ•ด๋‹น์ฝ”๋“œ๋Š” ์šฐ์„  ์ž…๋ ฅ, ์žฌ๊ท€๋ฅผ ์œ„ํ•ด ํ•ญ์ƒ ํ•„์š”ํ•œ ์ฝ”๋“œ์ด๋‹ค.

 

for _ in range(int(sys.stdin.readline())):
    m, n, k = map(int, sys.stdin.readline().split())
    field = [[0 for _ in range(m)] for _ in range(n)]
    count = 0

 

ํ•ด๋‹น ์ฝ”๋“œ๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜ ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, ๋ฐฐ์ถ” ๋ฐญ ๊ฐ€๋กœ, ์„ธ๋กœ, ๋ฐฐ์ถ” ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ๋ฐฐ์ถ” ๋ฐญ ํฌ๊ธฐ๋งŒํผ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

 

for _ in range(int(sys.stdin.readline())):
    m, n, k = map(int, sys.stdin.readline().split())
    field = [[0 for _ in range(m)] for _ in range(n)]
    count = 0
    for _ in range(k):
        x, y = map(int, sys.stdin.readline().split())
        field[y][x] = 1

 

ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๋ฐฐ์ถ”์ˆ˜ ๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์„œ ๋ฐฐ์ถ” ์ขŒํ‘œ ์ž…๋ ฅ์„ ๋ฐ›๊ณ  ๋ฆฌ์ŠคํŠธ์— ํ‘œ์‹œ๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

for _ in range(int(sys.stdin.readline())):
    m, n, k = map(int, sys.stdin.readline().split())
    field = [[0 for _ in range(m)] for _ in range(n)]
    count = 0
    for _ in range(k):
        x, y = map(int, sys.stdin.readline().split())
        field[y][x] = 1
    for x in range(m):
        for y in range(n):
            if field[y][x] == 1:
                dfs(x, y)
                count += 1

 

ํ•ด๋‹น์ฝ”๋“œ๋Š” 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ๋ฐฐ์ถ”๋ฅผ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, ๋งŒ์•ฝ ๋ฐฐ์ถ”์ผ ๊ฒฝ์šฐ dfs๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๋ฒŒ๋ ˆ ํ•œ๋งˆ๋ฆฌ๋ฅผ ์นด์šดํŠธํ•  ์ˆ˜

์žˆ๋‹ค.

 

def dfs(x, y):
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]

 

ํ•ด๋‹น์ฝ”๋“œ๋Š” dfsํ•จ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ์ขŒํ‘œ๋ฅผ ์œ„ํ•œ dx,dy ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธํ•˜๊ธฐ์œ„ํ•จ์ด๋‹ค.

 

def dfs(x, y):
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]
    
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]

 

ํ•ด๋‹น ์ฝ”๋“œ๋Š” x,y์˜ ์ƒํ•˜์ขŒ์šฐ ์ขŒํ‘œ nx,ny ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

def dfs(x, y):
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]
    
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if (0 <= nx < m) and (0 <= ny < n):
            if field[ny][nx] == 1:
                field[ny][nx] = 0
                dfs(nx, ny)

 

nx, ny ๊ฐ€ ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค ๋ฒ”์œ„ ์•ˆ์— ์žˆ๋Š”์ง€ ๊ฒ€์ฆํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ๋ฐฐ์ถ”๋ฅผ ๊ฒ€์ฆํ•˜๋Š”๋ฐ, ๋ฐฐ์ถ”๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ๋ฐฉ๋ฌธ ํ‘œ์‹œํ•˜์—ฌ ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ ํ›„ ํ•ด๋‹น ์ขŒํ‘œ๋กœ dfs ์žฌ๊ท€ํ˜ธ์ถœ(๋˜๋‹ค๋ฅธ ์ƒํ•˜์ขŒ์šฐ ์ขŒํ‘œ ๊ฒ€์ฆ)์„ ํ•œ๋‹ค.

 

๋งˆ์ง€๋ง‰์€ printf๋กœ ๋ฒŒ๋ ˆ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

๐Ÿ“Œ ๋Š๋‚€์ 

 

2์ฐจ์› ๋ฐฐ์—ด๊ณผ ๊ด€๋ จํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ฌธ์ œ์ธ๋ฐ ์ž‘๋…„ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋•Œ ๋ฐฐ์šด ์ดํ›„ ๋ณต์Šตํ•˜์ง€ ์•Š์•„์„œ ๊ทธ๋Ÿฐ์ง€ ์•„์ง ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™๋‹ค.

๋ฐ˜๋ณต๋ฌธ๊ณผ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ ์ด์™ธ์— ์ฝ”๋“œ๋ฅผ ์งœ๋Š”๋ฐ ๋„์›€์ด ์—†์ด๋Š” ์–ด๋ ค์› ๊ณ , ์ต์ˆ™ํ•ด์ง€๋„๋ก ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ๊พธ์ค€ํžˆ ํ‘ธ๋Š”๊ฒŒ ์ค‘์š”ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

 

 

[์ฐธ๊ณ ํ•œ ์ž๋ฃŒ]

 

https://develop247.tistory.com/239

 

[ํŒŒ์ด์ฌ/Python] ๋ฐฑ์ค€ 1012๋ฒˆ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

[ํŒŒ์ด์ฌ/Python] ๋ฐฑ์ค€ 1012๋ฒˆ ์œ ๊ธฐ๋† ๋ฐฐ์ถ” 1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ” ์ฐจ์„ธ๋Œ€ ์˜๋†์ธ ํ•œ๋‚˜๋Š” ๊ฐ•์›๋„ ๊ณ ๋žญ์ง€์—์„œ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋†์•ฝ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋ ค๋ฉด ๋ฐฐ์ถ”๋ฅผ ํ•ด์ถฉ์œผ๋กœ๋ถ€

develop247.tistory.com