https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
from collections import deque
n, m = map(int, input().split())
walk = 0
def bfs(x,y):
queue = deque()
queue.append((x,y))
dx = [-1,1,0,0]
dy = [0,0,-1,1]
while queue:
x,y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx >=0 and ny >=0 and ny < m and nx <n:
if graph[nx][ny] == 1:
graph[nx][ny] = graph[x][y]+1
queue.append((nx,ny))
return graph[n-1][m-1]
graph = []
for i in range(n):
graph.append(list(map(int,input())))
print(bfs(0,0))
반응형
'알고리즘 공부 > BFS & DFS' 카테고리의 다른 글
[백준] 7562번 나이트의 이동 - 파이썬 (0) | 2022.02.19 |
---|---|
[백준] 7576번 토마토 - 파이썬 (0) | 2022.02.19 |
[백준] 1012번 유기농 배추 - 파이썬 (0) | 2021.08.18 |
[백준] 2606번 단지번호붙이기 - 파이썬 (0) | 2021.08.17 |
[백준] 2606번 바이러스 - 파이썬 (0) | 2021.08.17 |