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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

16197๋ฒˆ: ๋‘ ๋™์ „

N×M ํฌ๊ธฐ์˜ ๋ณด๋“œ์™€ 4๊ฐœ์˜ ๋ฒ„ํŠผ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฒŒ์ž„์ด ์žˆ๋‹ค. ๋ณด๋“œ๋Š” 1×1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์นธ์€ ๋น„์–ด์žˆ๊ฑฐ๋‚˜, ๋ฒฝ์ด๋‹ค. ๋‘ ๊ฐœ์˜ ๋นˆ ์นธ์—๋Š” ๋™์ „์ด ํ•˜๋‚˜์”ฉ ๋†“์—ฌ์ ธ ์žˆ๊ณ , ๏ฟฝ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 NxM ํฌ๊ธฐ์˜ ๋ณด๋“œ์™€ 4๊ฐœ์˜ ๋ฒ„ํŠผ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฒŒ์ž„์—์„œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋™์ „์„ ๋ณด๋“œ ๋ฐ–์œผ๋กœ ๋ณด๋‚ด๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฒ„ํŠผ์€ ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ 2๊ฐœ์˜ ๋™์ „์„ ๋™์‹œ์— ์›€์ง์ด๊ฒŒ ํ•œ๋‹ค. ์ด๋•Œ ์ตœ์†Œ๋กœ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋™์ „์„ ๋ณด๋“œ ๋ฐ–์œผ๋กœ ๋ณด๋‚ด๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

 ์ด ๋ฌธ์ œ๋Š” ์•ž์„œ ๋‹ค๋ฃฌ, ๊ตฌ์Šฌ ํƒˆ์ถœ, ๊ตฌ์Šฌ ํƒˆ์ถœ 3์™€ ์ƒ๋‹นํžˆ ์œ ์‚ฌํ•œ ๋ฌธ์ œ์ด๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์„ ๊ธฐ์–ตํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋ฌธ์ œ ์—ญ์‹œ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ œ๋Š” ๋‹ค์Œ ๋กœ์ง๊ณผ ๊ฐ™์ด ์ ‘๊ทผํ•˜๋ฉด ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

  • BFS๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰์„ ํ•œ๋‹ค.
  • ๋™์ „์„ ๋™์‹œ์— ์ƒ, ํ•˜, ์ขŒ, ์šฐ ์ขŒํ‘œ ์ค‘ ํ•˜๋‚˜๋กœ ์›€์ง์ธ๋‹ค.
    • ์‹œ๋„ ํšŸ์ˆ˜๊ฐ€ 10ํšŒ ์ด์ƒ์ด๋ผ๋ฉด ์ค‘๋‹จํ•˜๊ณ , -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • ๋‘ ๋™์ „ ๋ชจ๋‘ ๋ฐ–์œผ๋กœ ๋–จ์–ด์ง„๋‹ค๋ฉด, ํƒ์ƒ‰์—์„œ ์ œ์™ธํ•œ๋‹ค.
    • ๋‘ ๋™์ „ ์ค‘ ํ•˜๋‚˜๋งŒ ๋ฐ–์œผ๋กœ ๋–จ์–ด์ง„๋‹ค๋ฉด ๋‹ต์„ ์ฐพ์€ ๊ฒƒ์ด๋ฏ€๋กœ, ์‹œ๋„ ํšŸ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • ๋™์ „์ด ๋ฒฝ๊ณผ ๋งŒ๋‚œ๋‹ค๋ฉด, ํ˜„์žฌ์˜ ๊ฒฝ๋กœ๋ฅผ ์œ ์ง€ํ•œ๋‹ค.
    • ๊ฐ ์ขŒํ‘œ์˜ ๊ฒฝ๋กœ๋ฅผ ๋ฐฉ๋ฌธํ•˜์˜€๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def search_coin():
    location = []
    for i in range(n):
        for j in range(m):
            if graph[i][j] == 'o':
                location.append([i, j])
    return location


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


def bfs(coins):
    q = deque([[coins[0], coins[1], 1]])
    visited[coins[0][X]][coins[0][Y]][coins[1][X]][coins[1][Y]] = True

    while q:
        one, two, cnt = q.popleft()

        # cnt๊ฐ€ 10์ด ๋„˜์–ด๋„ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ.
        if cnt > 10:
            break

        for dx, dy in dirs:
            # ๊ฐ๊ฐ์˜ ๋™์ „์„ ์ด๋™ ์‹œํ‚ด.
            next_one = [one[X] + dx, one[Y] + dy]
            next_two = [two[X] + dx, two[Y] + dy]

            # ๋‘ ๋™์ „ ๋ชจ๋‘, ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์€ ์œ„์น˜๋กœ ๊ฐ„๋‹ค๋ฉด ํƒ์ƒ‰ํ•˜์ง€ ์•Š์•„๋„ ๋จ.
            if not visitable(*next_one) and not visitable(*next_two):
                continue

            # ๋™์ „1์˜ ๋‹ค์Œ ์œ„์น˜๊ฐ€ ๋ฐ–์ด๊ฑฐ๋‚˜, ๋™์ „2์˜ ๋‹ค์Œ ์œ„์น˜๊ฐ€ ๋ฐ–์ธ ๊ฒฝ์šฐ
            # ์ตœ์†Œ ๋ฒ„ํŠผ ํด๋ฆญ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒ.
            if visitable(*next_one) and not visitable(*next_two) or \
                    not visitable(*next_one) and visitable(*next_two):
                return cnt

            # ๋™์ „์ด ๋ฒฝ๊ณผ ๋งŒ๋‚œ ๊ฒฝ์šฐ, ํ˜„์žฌ ์œ„์น˜์—์„œ ๊ณ ์ •.
            if graph[next_one[X]][next_one[Y]] == '#':
                next_one = one
            if graph[next_two[X]][next_two[Y]] == '#':
                next_two = two

            # ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๊ฒฝ๋กœ ํƒ์ƒ‰์„ ์œ„ํ•ด ํ์— ์ถ”๊ฐ€ํ•จ.
            if not visited[next_one[X]][next_one[Y]][next_two[X]][next_two[Y]]:
                visited[next_one[X]][next_one[Y]][next_two[X]][next_two[Y]] = True
                q.append([next_one, next_two, cnt + 1])
    return -1


if __name__ == '__main__':
    X, Y = 0, 1
    dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    n, m = map(int, stdin.readline().split())
    graph = [list(stdin.readline().rstrip()) for _ in range(n)]
    # ๋ฐฉ๋ฌธ ์ƒํƒœ๋ฅผ ๋™์ „ ๋‘๊ฐœ์˜ ์ขŒํ‘œ์— ๋”ฐ๋ผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด 4์ฐจ์› ๋ฐฐ์—ด ์„ ์–ธ.
    visited = \
        [[[[False] * m for _ in range(n)] for _ in range(m)] for _ in range(n)]

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