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

728x90
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ

 

3055๋ฒˆ: ํƒˆ์ถœ

๋ฌธ์ œ ์‚ฌ์•…ํ•œ ์•”ํ‘์˜ ๊ตฐ์ฃผ ์ด๋ฏผํ˜์€ ๋“œ๋””์–ด ๋งˆ๋ฒ• ๊ตฌ์Šฌ์„ ์†์— ๋„ฃ์—ˆ๊ณ , ๊ทธ ๋Šฅ๋ ฅ์„ ์‹คํ—˜ํ•ด๋ณด๊ธฐ ์œ„ํ•ด ๊ทผ์ฒ˜์˜ ํ‹ฐ๋–ฑ์ˆฒ์— ํ™์ˆ˜๋ฅผ ์ผ์œผํ‚ค๋ ค๊ณ  ํ•œ๋‹ค. ์ด ์ˆฒ์—๋Š” ๊ณ ์Šด๋„์น˜๊ฐ€ ํ•œ ๋งˆ๋ฆฌ ์‚ด๊ณ  ์žˆ๋‹ค. ๊ณ ์Šด๋„์น˜๏ฟฝ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๊ณ ์Šด๋„์น˜(S)๊ฐ€ ๋น„๋ฒ„์˜ ๊ตด(D)๋กœ ํƒˆ์ถœ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‹ค๋ฅธ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์™€ ๋‹ค๋ฅธ ์ ์€ ์‹œ๊ฐ„์— ๋”ฐ๋ผ ๋ฌผ(*)์ด ๋น„์–ด ์žˆ๋Š” ์นธ์œผ๋กœ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ€๋งŒํžˆ ๋“ค์—ฌ๋‹ค ๋ณด๋ฉด ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์•„๋‹ˆ๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

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

 ์ฆ‰, ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•  ํ•„์š” ์—†์ด ๋ฌผ์ด ํ™•์žฅํ•˜๋Š” ๊ฒƒ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค๋ฉด์„œ ๊ณ ์Šด ๋„์น˜๊ฐ€ ํƒˆ์ถœ ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋‹จํ•˜๋ฉด ๋˜๋Š” 2๋ฒˆ์˜ BFS ํƒ์ƒ‰์ด ํ•„์š”ํ•œ ๋ฌธ์ œ์ด๋‹ค.

 

์ฝ”๋“œ

from collections import deque
from sys import stdin


def visitable(x, y):
    return 0 <= x < n and 0 <= y < m


def bfs(start):
    flood()
    q = deque([start])

    while q:
        for _ in range(len(q)):
            cur_x, cur_y = q.popleft()

            for x, y in dirs:
                next_x = cur_x + x
                next_y = cur_y + y
                if visitable(next_x, next_y):
                    if graph[next_x][next_y] == '.' \
                            and not visited[next_x][next_y]:
                        visited[next_x][next_y] = visited[cur_x][cur_y] + 1
                        q.append([next_x, next_y])
                    elif graph[next_x][next_y] == 'D':
                        return visited[cur_x][cur_y]
        flood()

    return "KAKTUS"


def flood():
    for _ in range(len(water)):
        cur_x, cur_y = water.popleft()
        for x, y in dirs:
            next_x = cur_x + x
            next_y = cur_y + y
            if visitable(next_x, next_y) and graph[next_x][next_y] == '.':
                graph[next_x][next_y] = '*'
                water.append([next_x, next_y])


if __name__ == '__main__':
    X, Y = 0, 1
    dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
    n, m = map(int, input().split())
    graph = []
    visited = [[0] * m for _ in range(n)]
    water = deque()

    for i in range(n):
        graph.append(list(stdin.readline().strip()))
        if 'S' in graph[i]:
            start = [i, graph[i].index('S')]
            visited[start[X]][start[Y]] = 1
        elif '*' in graph[i]:
            water.append([i, graph[i].index('*')])

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