求助 python全RE

P1746 离开中山路

jzr21894 @ 2024-04-07 20:39:44

def bfs(x, y):
    queue.append([x, y])
    dist[x][y] = 0

    while len(queue) != 0:
        t = queue.pop(0)
        for i in range(4):
            a = t[0] + dx[i]
            b = t[1] + dy[i]

            if a<0 or a>x2 or b<0 or b>y2:
                continue
            if g[a][b] != 0:
                continue
            if dist[a][b] > 0:
                continue

            queue.append([a,b])
            dist[a][b] = dist[t[0]][t[1]] + 1
    return dist[x2][y2]

N = 1010
n = int(input())

g = [[0] * N for _ in range(N)]
for i in range(1,n+1):
    g[i][1:n + 1] = [int(c) for c in input()]
x1, y1, x2, y2 = map(int, input().split())
dist = [[-1] * N for _ in range(N)]
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
queue = []

res = bfs(x1, y1)
print(res)

|