https://www.acmicpc.net/problem/2178
#2178_미로탐색
from collections import deque
N, M = map(int, input().split())
maze = []
for _ in range(N):
maze.append(list(map(int, input())))
def bfs(x,y):
dx=[-1,1,0,0]
dy=[0,0,-1,1]
q=deque()
q.append((x,y))
while q:
x,y=q.popleft()
for i in range(4):
nx = x+dx[i]
ny = y+dy[i]
if nx < 0 or nx >= N or ny < 0 or ny >= M:
continue
if maze[nx][ny]!=0:
if maze[nx][ny]==1:
maze[nx][ny]=maze[x][y]+1
q.append((nx,ny))
return maze[N-1][M-1]
print(bfs(0,0))
'알고리즘 > 백준(BOJ)' 카테고리의 다른 글
[백준/알고리즘] #1316: 그룹단어체커 [파이썬(python)/문자열] (0) | 2021.10.18 |
---|---|
[백준/알고리즘] #17609: 회문 [파이썬(python)/문자열] (0) | 2021.10.18 |
[백준/알고리즘] #1110: 더하기 사이클 [파이썬(python)/수학] (0) | 2021.10.18 |
[백준/알고리즘] #5639: 이진검색트리 [파이썬(python)/트리] (0) | 2021.10.18 |
[백준/알고리즘] #1991: 트리순회 [파이썬(python)/트리] (0) | 2021.10.18 |