๊ธฐ๋ก๋ฐฉ

BOJ_1012 : ์œ ๊ธฐ๋† ๋ฐฐ์ถ” ๋ณธ๋ฌธ

CodingTest/Python

BOJ_1012 : ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

Soom_1n 2022. 9. 15. 00:04

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

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

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ง€๋ ์ด๊ฐ€ ํผ์ง€๋Š” ๋ฒ”์œ„๋Š” ๋ฐฐ์ถ”๊ฐ€ ๊ฐ€๋กœ์„ธ๋กœ๋กœ ๋ถ™์–ด์žˆ๋Š” ๋ฒ”์œ„๋ฅผ ๋œปํ•œ๋‹ค.
  • ๋ถ™์–ด์žˆ๋Š” ๋ฐฐ์ถ” ๊ทธ๋ฃน์ด ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import sys
from collections import deque

dxy = [[0,-1],[-1,0],[0,1],[1,0]]

for _ in range(int(sys.stdin.readline())):
    m, n ,k = map(int, sys.stdin.readline().rstrip().split())

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

    answer = 0
    visit = [[False] * m for _ in range(n)]

    for i in range(n):
        for j in range(m):
            que = deque()
            if field[i][j] == 1 and visit[i][j] == False:
                que.append([i,j])
                field[i][j] = 0
                while len(que) > 0:
                    x, y = que.popleft()
                    visit[x][y] = True
                    for k in range(4):
                        dx = x + dxy[k][0]
                        dy = y + dxy[k][1]
                        
                        if dx < 0 or dx >= n or dy < 0 or dy >= m:
                            continue
                        
                        if field[dx][dy] == 1 and visit[dx][dy] == False:
                            field[dx][dy] = 0
                            que.append([dx,dy])
                answer += 1
    print(answer)

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • BFS๋ฅผ ํ†ตํ•ด์„œ ๊ฐ™์€ ๊ทธ๋ฃน์˜ ๋ฐฐ์ถ”๋ฅผ ์ฐพ๊ณ , ๊ทธ๋ฃน์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • m, n, k๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  n,m ์œผ๋กœ ์ „์ฒด ๋ฐญ์„ ์กฐ์‚ฌํ•ด์„œ 1์„ ์ฐพ๋Š”๋‹ค.
    • 1์„ ์ฐพ์€ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ BFS๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.
      • ํ•ด๋‹น ์ขŒํ‘œ์—์„œ ์ขŒ ์ƒ ์šฐ ํ•˜ ์ธ๋ฑ์Šค๋ฅผ ํ™•์ธํ•ด ๋ฆฌ์ŠคํŠธ ๋ฒ”์œ„ ๋‚ด์ธ์ง€, ๊ฐ’์€ 1์ด๊ณ  ๋ฐฉ๋ฌธํ•œ์  ์—†๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
      • ๋ฒ”์œ„๋‚ด์ด๋ฉฐ ๊ฐ’์ด 1์ด๊ณ  ๋ฐฉ๋ฌธํ•œ์  ์—†์œผ๋ฉด, ๊ฐ’์„ 0์œผ๋กœ ๋งŒ๋“ค๊ณ  ํ์— ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ ๋„ฃ๋Š”๋‹ค.
      • ํ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • ํ์˜ ๋ฐ˜๋ณต์ด ๋๋‚˜ BFS๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๊ทธ๋ฃน์˜ ์ˆ˜๋ฅผ 1 ๋”ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

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

728x90