BOJ - 6087 - 레이저 통신

Updated:

from collections import deque
import sys

def BFS(Q, Laser, Check, N, M):
    dx = [0, 0, 1, -1]
    dy = [1, -1, 0, 0]

    while Q:
        x, y = Q.popleft()

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            while 0 <= nx < N and 0 <= ny < M and Laser[nx][ny] != '*':
                if not Check[nx][ny]:
                    Check[nx][ny] = Check[x][y] + 1
                    Q.append((nx, ny))
                nx, ny = nx + dx[i], ny + dy[i]

def FindMirror(Laser, N, M):
    flag = True
    start = [0, 0]
    end = [0, 0]
    for idx in range(N):
        for jdx in range(M):
            if Laser[idx][jdx] == 'C':
                if flag:
                    start[0] = idx
                    start[1] = jdx
                    flag = False
                else:
                    end[0] = idx
                    end[1] = jdx
                    return start, end

def solution():
    Q = deque()
    W, H = map(int, sys.stdin.readline().split())
    Laser = []
    Check = [[0 for _ in range(W)] for i in range(H)]

    for i in range(H):
        Laser.append(str(input()))

    start, end = FindMirror(Laser, H, W)
    Q.append((start[0], start[1]))
    BFS(Q, Laser, Check, H, W)
    print(Check[end[0]][end[1]] - 1)

solution()

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

Categories:

Updated:

Leave a comment