문제
https://www.acmicpc.net/problem/2178
코드
from collections import deque
queue=deque()
n,m=map(int, input().split())
graph=[list(map(int, list(input().rstrip()))) for _ in range(n)]
visited=[[0]*m for _ in range(n)]
dx=[0,0,-1,1]
dy=[-1,1,0,0]
queue.append([0,0])
while queue:
x,y=queue.popleft()
if x==n-1 and y==m-1:
print(visited[x][y]+1)
exit(0)
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 graph[nx][ny]==1:
visited[nx][ny]=visited[x][y]+1
queue.append([nx,ny])
'문제풀이 > DFS & BFS' 카테고리의 다른 글
@[파이썬] [DFS] 백준 2468 안전 영역 (0) | 2022.12.29 |
---|---|
@[파이썬] [DFS] 백준 1012 유기농 배추 (0) | 2022.12.27 |
[파이썬] [DFS] 백준 1759 암호 만들기 (0) | 2022.05.27 |
[PyPy] [BFS] 백준 9019 DSLR (0) | 2022.05.26 |
[PyPy] [DFS] 백준 15684 사다리 조작 (0) | 2022.05.19 |