코딩테스트/백준문제

[python 파이썬] 백준 2178번 : 미로 탐색

jaewon_sss 2020. 8. 31. 23:55
반응형
import sys
from collections import deque
input = sys.stdin.readline

n, m = map(int, input().split())
q = deque()
miro = [list(map(int,input().strip())) for _ in range(n)]
visited = [[0]*m for _ in range(n)]

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

def bfs():
while q:
x, y = q.popleft()
if x == n - 1 and y == m - 1:
print(visited[x][y])
break

for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if visited[nx][ny] == 0 and miro[nx][ny] == 1:
visited[nx][ny] = visited[x][y] + 1
q.append((nx, ny))

visited[0][0] = 1
q.append((0,0))

bfs()


반응형