ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

2573๋ฒˆ: ๋น™์‚ฐ

์ฒซ ์ค„์—๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด์˜ ํ–‰์˜ ๊ฐœ์ˆ˜์™€ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ N๊ณผ M์ด ํ•œ ๊ฐœ์˜ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 3 ์ด์ƒ 300 ์ดํ•˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„๋งˆ๋‹ค ๋ฐฐ์—ด์˜ ๊ฐ ํ–‰์„ ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋น™์‚ฐ์˜ ๋†’์ด๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, 1๋…„์ด ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ๋ฐ”๋‹ค์— ์ธ์ ‘ํ•œ ์ˆ˜๋งŒํผ ๋น™์‚ฐ์ด ๋…น์•„์„œ ์—†์–ด์ง„๋‹ค. ์ด๋•Œ ๋น™์‚ฐ์˜ ์˜์—ญ์ด 2๊ฐœ ์ด์ƒ์ด ๋˜๋Š” ์ตœ์†Œ์˜ ์‹œ๊ฐ„(๋…„)์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” BFS๋ฅผ ํ†ตํ•ด ์˜์—ญ์„ ํƒ์ƒ‰ํ•จ๊ณผ ๋™์‹œ์—, ๊ฐ ๋น™ํ•˜๊ฐ€ ๋ฐ”๋‹ค์™€ ์ธ์ ‘ํ•˜๊ณ  ์žˆ๋Š” ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • BFS๋ฅผ ํ†ตํ•ด, ๋น™ํ•˜์ธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.
    • ๋น™ํ•˜ ์ฃผ์œ„๊ฐ€ ๋ฐ”๋‹ค๋ผ๋ฉด, ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ง€์ ์„ ์นด์šดํŠธํ•œ๋‹ค.
  • BFS ํƒ์ƒ‰ ํ›„์—, ํƒ์ƒ‰ ๊ฐ€๋Šฅํ•œ ์˜์—ญ (๋น™ํ•˜์˜ ์˜์—ญ)์ด 2๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด, ์ฆ‰์‹œ ์ค‘๋‹จํ•˜๊ณ  ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๋น™ํ•˜๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด, 0์„ ์ถœ๋ ฅํ•˜๊ณ  ํƒ์ƒ‰์„ ์ข…๋ฃŒํ•œ๋‹ค.
  • ๋น™ํ•˜๊ฐ€ ๋‚จ์•„ ์žˆ๊ณ , ์˜์—ญ์ด 2๊ฐœ ์ด์ƒ์ด ์•„๋‹ˆ๋ผ๋ฉด ์นด์šดํŠธํ•œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋น™ํ•˜๋ฅผ ๋…น์ธ๋‹ค.
  • ์œ„์˜ ๊ณผ์ •์„ ๋น™ํ•˜๊ฐ€ ์—†๊ฑฐ๋‚˜, ์˜์—ญ์„ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque, defaultdict


def visitable(x, y):
    return 0 <= x < n and 0 <= y < m and not visited[x][y]


def bfs(start):
    ret = defaultdict(int)
    q = deque([start])
    dirs = ((0, 1), (0, -1), (-1, 0), (1, 0))

    while q:
        x, y = q.popleft()

        for dx, dy in dirs:
            next_x, next_y = x + dx, y + dy
            if visitable(next_x, next_y):
                # ์ฃผ๋ณ€์ด ๋ฐ”๋‹ค์ธ ๊ฒฝ์šฐ, ๋ฐ”๋‹ค์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธ
                if graph[next_x][next_y] == 0:
                    ret[(x, y)] += 1
                # 0์ด ์•„๋‹ˆ๊ณ  ๋น™ํ•˜์ธ ๊ฒฝ์šฐ ํƒ์ƒ‰์„ ์œ„ํ•ด ์ถ”๊ฐ€
                else:
                    visited[next_x][next_y] = True
                    q.append((next_x, next_y))
    return ret


if __name__ == '__main__':
    n, m = map(int, stdin.readline().split())
    graph = [list(map(int, stdin.readline().split())) for _ in range(n)]

    time = 0

    while True:
        visited = [[False] * m for _ in range(n)]
        iceberg = 0
        for i in range(n):
            for j in range(m):
                if graph[i][j] != 0 and not visited[i][j]:
                    visited[i][j] = True
                    ice = bfs([i, j])
                    iceberg += 1
                    # ๋น™์‚ฐ์ด 2๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ ๊ฒฝ์šฐ
                    if iceberg > 1:
                        print(time)
                        exit(0)

        # ๋น™์‚ฐ์ด ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ, ์ค‘๋‹จ.
        if iceberg == 0:
            time = 0
            break

        # ์ฃผ๋ณ€์˜ ๋ฐ”๋‹ค ์ˆ˜๋งŒํผ ๋น™ํ•˜๋ฅผ ๋…น์ž„.
        for (i, j), cnt in ice.items():
            graph[i][j] = 0 if graph[i][j] < cnt else graph[i][j] - cnt

        time += 1

    print(time)

 ์ฒ˜์Œ์— ํ’€ ๋•Œ๋Š” ํ•˜๋‚˜๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋น™ํ•˜๋ฅผ ๋ฐ”๋กœ๋ฐ”๋กœ ๋…น์˜€๋Š”๋ฐ, ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์˜ˆ์ œ ์ž…๋ ฅ 1์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์˜์—ญ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒธ, ๋น™ํ•˜๋ฅผ ๋…น์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์นด์šดํŠธํ•˜์—ฌ์•ผ ํ•œ๋‹ค.๐Ÿ˜‰

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€