[백준] 13459번 구슬 탈출

HL·2021년 7월 17일
0

백준

목록 보기
104/104

링크

https://www.acmicpc.net/problem/13459

코드

import sys
from itertools import product

dy, dx = [-1, 1, 0, 0], [0, 0, -1, 1]
board = [[]]


def solution():
    global board

    read = sys.stdin.readline
    h, w = map(int, read().split())
    origin_board = [list(read().rstrip()) for _ in range(h)]
    for y in range(h):
        for x in range(w):
            if origin_board[y][x] == 'B':
                sby, sbx = y, x
            elif origin_board[y][x] == 'R':
                sry, srx = y, x
            elif origin_board[y][x] == 'O':
                oy, ox = y, x

    possible = False
    for prod in product([2,3,0,1], repeat=10):
        board = [origin_board[i][:] for i in range(h)]
        by, bx, ry, rx = sby, sbx, sry, srx
        for d in prod:
            by, bx, ry, rx = move(by, bx, ry, rx, d)
            if by == oy and bx == ox:
                break
            elif ry == oy and rx == ox:
                possible = True
                break
        if possible:
            break

    if possible:
        print(1)
    else:
        print(0)


def move(by, bx, ry, rx, d):

    # 상
    if d == 0:
        if bx == rx:
            # b먼저
            if by < ry:
                by, bx = move_blue(by, bx, d)
                ry, rx = move_red(ry, rx, d)
            else:
                ry, rx = move_red(ry, rx, d)
                by, bx = move_blue(by, bx, d)
        else:
            ry, rx = move_red(ry, rx, d)
            by, bx = move_blue(by, bx, d)

    # 하
    elif d == 1:
        if bx == rx:
            # r먼저
            if by < ry:
                ry, rx = move_red(ry, rx, d)
                by, bx = move_blue(by, bx, d)
            else:
                by, bx = move_blue(by, bx, d)
                ry, rx = move_red(ry, rx, d)
        else:
            by, bx = move_blue(by, bx, d)
            ry, rx = move_red(ry, rx, d)

    # 좌
    elif d == 2:
        if by == ry:
            # b먼저
            if bx < rx:
                by, bx = move_blue(by, bx, d)
                ry, rx = move_red(ry, rx, d)
            else:
                ry, rx = move_red(ry, rx, d)
                by, bx = move_blue(by, bx, d)
        else:
            ry, rx = move_red(ry, rx, d)
            by, bx = move_blue(by, bx, d)

    # 우
    elif d == 3:
        if by == ry:
            # r먼저
            if bx < rx:
                ry, rx = move_red(ry, rx, d)
                by, bx = move_blue(by, bx, d)
            else:
                by, bx = move_blue(by, bx, d)
                ry, rx = move_red(ry, rx, d)
        else:
            by, bx = move_blue(by, bx, d)
            ry, rx = move_red(ry, rx, d)

    return by, bx, ry, rx

'''
. B R O #
'''


def move_blue(sy, sx, d):
    global board

    cy, cx = sy, sx
    while True:
        ny, nx = cy + dy[d], cx + dx[d]
        if board[ny][nx] == '#' or board[ny][nx] == 'R' or board[ny][nx] == 'B':
            board[sy][sx] = '.'
            board[cy][cx] = 'B'
            return cy, cx
        if board[ny][nx] == 'O':
            board[sy][sx] = '.'
            return ny, nx

        cy, cx = ny, nx


def move_red(sy, sx, d):
    global board

    cy, cx = sy, sx
    while True:
        ny, nx = cy + dy[d], cx + dx[d]
        if board[ny][nx] == '#' or board[ny][nx] == 'R' or board[ny][nx] == 'B':
            board[sy][sx] = '.'
            board[cy][cx] = 'R'
            return cy, cx
        if board[ny][nx] == 'O':
            board[sy][sx] = '.'
            return ny, nx
        cy, cx = ny, nx


solution()
profile
Frontend 개발자입니다.

0개의 댓글