jjQIANG @ 2024-03-28 18:10:05
def bfs(x1,y1,x2,y2,maze,n,maze_dis):
queue = []
queue.append((x1,y1))
move = [
lambda x,y:(x-1,y),
lambda x,y:(x+1,y),
lambda x,y:(x,y-1),
lambda x,y:(x,y+1)
]
while len(queue) != 0:
cur = queue.pop(0)
if cur[0] == x2 and cur[1] == y2:
print(maze_dis[cur[0]-1][cur[1]-1])
return
for m in move:
next = m(cur[0],cur[1])
if next[0] < 1 or next[0] > n:continue
if next[1] > n or next[1] < 1:continue
if maze[next[0]-1][next[1]-1] == 1:continue
if maze_dis[next[0]-1][next[1]-1] != 0:continue
queue.append(next)
maze_dis[next[0]-1][next[1]-1] = maze_dis[cur[0] - 1][cur[1] - 1] + 1
return
n = int(input())
maze = [list(int(i) for i in input()) for i in range(n)]
maze_dis = [list(0 for j in range(n)) for k in range(n)]
x1,y1,x2,y2 = map(int,input().split())
bfs(x1,y1,x2,y2,maze,n,maze_dis)
就很不明白为什么是RE,求助大佬