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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

13459๋ฒˆ: ๊ตฌ์Šฌ ํƒˆ์ถœ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋ณด๋“œ์˜ ์„ธ๋กœ, ๊ฐ€๋กœ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋‘ ์ •์ˆ˜ N, M (3 ≤ N, M ≤ 10)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์— ๋ณด๋“œ์˜ ๋ชจ์–‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธธ์ด M์˜ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์€ '.', '#', 'O', 'R', 'B'

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

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

 

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

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def move_beads(loc, dx, dy):
    cnt = 0
    while graph[loc[X] + dx][loc[Y] + dy] != '#':
        if graph[loc[X]][loc[Y]] == 'O':
            break
        loc = [loc[X] + dx, loc[Y] + dy]
        cnt += 1
    return loc, cnt


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

    while q:
        red, blue, cnt = q.popleft()

        if cnt > 10:
            break

        for dx, dy in dirs:
            next_red, red_cnt = move_beads(red, dx, dy)
            next_blue, blue_cnt = move_beads(blue, dx, dy)
            # ํŒŒ๋ž€์ƒ‰ ๊ตฌ์Šฌ์ด ๋จผ์ € ํƒˆ์ถœํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ œ์™ธ.
            if graph[next_blue[X]][next_blue[Y]] == 'O':
                continue
            # ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๋จผ์ € ํƒˆ์ถœ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
            if graph[next_red[X]][next_red[Y]] == 'O':
                return 1

            if next_red == next_blue:
                if red_cnt > blue_cnt:
                    next_red = [next_red[X] - dx, next_red[Y] - dy]
                else:
                    next_blue = [next_blue[X] - dx, next_blue[Y] - dy]

            r_x, r_y = next_red; b_x, b_y = next_blue

            if not visited[r_x][r_y][b_x][b_y]:
                visited[r_x][r_y][b_x][b_y] = True
                q.append([[r_x, r_y], [b_x, b_y], cnt + 1])

    return 0


if __name__ == '__main__':
    X, Y = 0, 1
    n, m = map(int, stdin.readline().split())
    graph = []
    red = blue = None
    for i in range(n):
        graph.append(list(stdin.readline().strip()))
        if red is None and 'R' in graph[i]:
            red = [i, graph[i].index('R')]
        if blue is None and 'B' in graph[i]:
            blue = [i, graph[i].index('B')]

    visited = \
        [[[[False] * m for _ in range(n)] for _ in range(m)] for _ in range(n)]
    visited[red[X]][red[Y]][blue[X]][blue[Y]] = True

    print(bfs([red, blue, 1]))

 ๊ตฌ์Šฌ ํƒˆ์ถœ 2์˜ ๋ฌธ์ œ๋Š” ํ•ด๋‹น ์ฝ”๋“œ์—์„œ bfs์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์ฐพ์ง€ ๋ชปํ•  ๊ฒฝ์šฐ -1, ์ฐพ์„ ๊ฒฝ์šฐ cnt ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.

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