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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

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

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

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž์„œ ๋‹ค๋ฃฌ ๋ฌธ์ œ์ธ ๊ตฌ์Šฌ ํƒˆ์ถœ์„ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด์ „์˜ ๋ฌธ์ œ์™€ ๋‹ค๋ฅธ ์ ์ด ์žˆ๋‹ค๋ฉด ๊ตฌ์Šฌ ํƒˆ์ถœ 2์˜ ๋ฌธ์ œ์—์„œ ์ด๋™ ๊ฒฝ๋กœ๋„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค. visited์˜ ๊ฒฝ์šฐ ๋‹จ์ˆœํžˆ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋Š” False / True ํ˜•ํƒœ๋กœ ์„ ์–ธํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, visited๋ฅผ ๋ฌธ์ž๋กœ ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ ๋ฐฉ๋ฌธ์— ๋”ฐ๋ผ ๋ฐฉ๋ฌธ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

dirs = {'R': (0, 1), 'L': (0, -1), 'D': (1, 0), 'U': (-1, 0)}

 ๊ธฐ์šธ์ธ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ, ๊ฒฝ๋กœ๋ฅผ ์ถ”์ ํ•˜๊ธฐ ์œ„ํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋ฉด ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

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 = {'R': (0, 1), 'L': (0, -1), 'D': (1, 0), 'U': (-1, 0)}

    while q:
        r, b, cnt = q.popleft()
        path = visited[r[X]][r[Y]][b[X]][b[Y]]

        if cnt > 10:
            break

        for move in dirs:
            dx, dy = dirs[move]
            next_path = path + move
            next_red, red_cnt = move_beads(r, dx, dy)
            next_blue, blue_cnt = move_beads(b, dx, dy)
            # ํŒŒ๋ž€์ƒ‰ ๊ตฌ์Šฌ์ด ๋จผ์ € ํƒˆ์ถœํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ œ์™ธ.
            if graph[next_blue[X]][next_blue[Y]] == 'O':
                continue
            # ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๋จผ์ € ํƒˆ์ถœ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
            if graph[next_red[X]][next_red[Y]] == 'O':
                # ๊ตฌ์Šฌ์„ ์ฐพ์€ ๊ฒฝ์šฐ
                # ์‹œ๋„ ํšŸ์ˆ˜, INIT ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋ฅผ ๋ฐ˜ํ™˜.
                return cnt, next_path[4:]

            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] = next_path
                q.append([[r_x, r_y], [b_x, b_y], cnt + 1])

    return -1, None


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 = \
        [[[[''] * m for _ in range(n)] for _ in range(m)] for _ in range(n)]
    visited[red[X]][red[Y]][blue[X]][blue[Y]] = 'INIT'

    for ret in bfs([red, blue, 1]):
        if ret is not None:
            print(ret)

 ์ดˆ๊ธฐ์— ๋นจ๊ฐ„ ๊ตฌ์Šฌ, ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ ๊ฒฝ๋กœ(์‹œ์ž‘์ )์— ์žฌ๋ฐฉ๋ฌธ ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด, ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ INIT๋กœ ์ดˆ๊ธฐํ™” ํ•˜์˜€๋‹ค. ๋”ฐ๋ผ์„œ ํƒˆ์ถœ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์—๋Š” INIT๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๊ฒฝ๋กœ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

 

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