AcWing 844. 走迷宫 - python 15行
原题链接
简单
作者:
roon2300
,
2020-07-18 21:32:26
,
所有人可见
,
阅读 366
n, m = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(n)]
dist = [[float('inf')] * m for _ in range(n)]
dist[0][0] = 0
q = [(0, 0)]
while q:
nq = []
for x, y in q:
for dx, dy in zip((-1, 0, 1, 0), (0, -1, 0, 1)):
nx, ny = x + dx, y + dy
if nx == n - 1 and ny == m - 1: print(dist[x][y] + 1); exit(0)
if 0 <= nx < n and 0 <= ny < m and mat[nx][ny] == 0 and dist[nx][ny] > dist[x][y] + 1:
dist[nx][ny] = dist[x][y] + 1
nq.append((nx, ny))
q = nq