Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Created July 30, 2025 08:07
Show Gist options
  • Select an option

  • Save inspirit941/d3ee651afead4f63b5f70a718a956da3 to your computer and use it in GitHub Desktop.

Select an option

Save inspirit941/d3ee651afead4f63b5f70a718a956da3 to your computer and use it in GitHub Desktop.
from collections import deque
def bfs(maps, start, end):
visited = set()
visited.add(start)
dirs = [(0,1),(0,-1),(1,0),(-1,0)]
queue = deque([start])
while queue:
y, x, cnt = queue.popleft()
if maps[y][x] == end:
return cnt
for dy, dx in dirs:
if 0 <= y+dy <len(maps) and 0 <= x+dx < len(maps[0]) and (y+dy, x+dx) not in visited and maps[y+dy][x+dx] != "X":
visited.add((y+dy, x+dx))
queue.append((y+dy, x+dx, cnt+1))
return -1
def solution(maps):
start = (0,0)
for y in range(len(maps)):
for x in range(len(maps[0])):
if maps[y][x] == "S":
start = (y,x,0)
if maps[y][x] == "L":
middle = (y,x,0)
# print(start, middle)
# 레버 위치까지 최솟값
min_middle = bfs(maps, start, "L")
if min_middle == -1:
return -1
min_exit = bfs(maps, middle, "E")
if min_exit == -1:
return -1
return min_middle + min_exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment