티스토리 뷰

https://school.programmers.co.kr/learn/courses/30/lessons/159993

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

from collections import deque
    
def bfs(start, end, maps):
    answer = 0
    
    dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
    row, col = len(maps), len(maps[0])
                              
    check = [[0] * col for _ in range(row)]
    
    for i in range(len(maps)):
        for j in range(len(maps[0])):
            if maps[i][j] == start:
                start = [i, j]
            elif maps[i][j] == end:
                end = [i, j]
    
    x, y = start[0], start[1]

    q = deque()
    q.append([x, y, 0])    
    
    while(q):
        x, y, res = q.popleft()
        
        if x == end[0] and y == end[1]:
            return res

        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            
            if 0 <= nx < row and 0 <= ny < col:
                if check[nx][ny] == 0 and maps[nx][ny] != "X":
                    check[nx][ny] = 1
                    q.append([nx, ny, res+1])
            
    return -1
                              
def solution(maps):
    to_lever = bfs("S", "L", maps)  # '시작 지점 -> 레버' 경로의 거리 구하기
    to_end = bfs("L", "E", maps)    # '레버 -> 출구' 경로의 거리 구하기
         
    if to_lever != -1 and to_end != -1:
        return to_lever + to_end
    
    return -1
공지사항
최근에 올라온 글
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함