백준 풀이

백준 2178번 파이썬 bfs easy

ag2개발자 2022. 8. 27. 18:43
from collections import deque
n,m=map(int,input().split())
graph=[]
for _ in range(n):
    graph.append(list(map(int,input())))
def bfs(x,y):
    dx =[-1,1,0,0 ]
    dy=[0,0,1,-1]
    queue = deque()
    queue.append((x,y))
    while queue:
        x,y=queue.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 graph[nx][ny]==1:
                graph[nx][ny]=graph[x][y]+1
                queue.append((nx,ny))
    return graph[n-1][m-1]
print(bfs(0,0))

'백준 풀이' 카테고리의 다른 글

백준 2667번 파이썬 bfs, dfs  (0) 2022.08.28
백준 2606번 파이썬 bfs easy  (0) 2022.08.28
백준 9095번 파이썬 (dp)  (0) 2022.08.27
백준 1003번 파이썬 (피보나치, dp)  (0) 2022.08.27
백준 11047번 파이썬 (그리디)  (0) 2022.08.27