n = int(input())
for i in range(n):
r,c,r1,c1,r2,c2 = map(int,input().split())
r1 -=1
c1 -=1
r2 -=1
c2 -=1
w = []
for j in range(r):
w.append([int(k) for k in input()])
w[r1][c1] = 1
#check top,bottom,left,right
qlist = [[r1,c1]]
qidx = 0
minlen = 0
while qidx < len(qlist):
ridx,cidx = qlist[qidx]
qidx +=1
if ridx-1>=0:
if w[ridx-1][cidx]!=1:
w[ridx-1][cidx] = w[ridx][cidx]+1
if ridx-1==r2 and cidx==c2:
minlen = w[ridx-1][cidx]
break
qlist.append([ridx-1,cidx])
if ridx+1<r:
if w[ridx+1][cidx]!=1:
w[ridx+1][cidx] = w[ridx][cidx]+1
if ridx+1==r2 and cidx==c2:
minlen = w[ridx+1][cidx]
break
qlist.append([ridx+1,cidx])
if cidx-1>=0:
if w[ridx][cidx-1]!=1:
w[ridx][cidx-1] = w[ridx][cidx]+1
if ridx==r2 and cidx-1==c2:
minlen = w[ridx][cidx-1]
break
qlist.append([ridx,cidx-1])
if cidx+1<c:
if w[ridx][cidx+1]!=1:
w[ridx][cidx+1] = w[ridx][cidx]+1
if ridx==r2 and cidx+1==c2:
minlen = w[ridx][cidx+1]
break
qlist.append([ridx,cidx+1])
# print(w)
print(minlen)
n = int(input())
for i in range(n):
r,c,r1,c1,r2,c2 = map(int,input().split())
r1 -=1
c1 -=1
r2 -=1
c2 -=1
w = []
for j in range(r):
w.append([int(k) for k in input()])
w[r1][c1] = 1
#check top,bottom,left,right
qlist = [[r1,c1]]
qidx = 0
minlen = 0
while qidx < len(qlist):
ridx,cidx = qlist[qidx]
qidx +=1
if ridx-1>=0:
if w[ridx-1][cidx]!=1:
w[ridx-1][cidx] = w[ridx][cidx]+1
if ridx-1==r2 and cidx==c2:
minlen = w[ridx-1][cidx]
break
qlist.append([ridx-1,cidx])
if ridx+1
if w[ridx+1][cidx]!=1:
w[ridx+1][cidx] = w[ridx][cidx]+1
if ridx+1==r2 and cidx==c2:
minlen = w[ridx+1][cidx]
break
qlist.append([ridx+1,cidx])
if cidx-1>=0:
if w[ridx][cidx-1]!=1:
w[ridx][cidx-1] = w[ridx][cidx]+1
if ridx==r2 and cidx-1==c2:
minlen = w[ridx][cidx-1]
break
qlist.append([ridx,cidx-1])
if cidx+1
if w[ridx][cidx+1]!=1:
w[ridx][cidx+1] = w[ridx][cidx]+1
if ridx==r2 and cidx+1==c2:
minlen = w[ridx][cidx+1]
break
qlist.append([ridx,cidx+1])
# print(w)
print(minlen)
已找到bug
上下左右下一步,判斷是否要進qlist只需判斷 w[][] == 0的才要進qlist